https://vuejs.org/guide/quick-start.html
代码编辑器:VSCode
浏览器开发工具(用于调试):Vue.js devtools
rkun1@LAPTOP-TUS5FU0D MINGW64 /c/code
$ mkdir VUE-START
rkun1@LAPTOP-TUS5FU0D MINGW64 /c/code
$ cd VUE-START/
rkun1@LAPTOP-TUS5FU0D MINGW64 /c/code/VUE-START
$ code .
创建index.html
文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<div id="app" class="container">
<h1>{{ pageTitle }}</h1>
<p>{{ content }}</p>
</div>
<script>
Vue.createApp(
{
data() {
return {
pageTitle: "Hello,Vue",
content: "Welcome to the wonderful world of Vue"
}
}
}
).mount('#app')
</script>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="link in links" class="nav-item">
<a class="nav-link" aria-current="page" href="#">{{ link }}</a>
</li>
</ul>
</div>
</nav>
<div id="app" class="container">
<h1>{{ pageTitle }}</h1>
<p>{{ content }}</p>
</div>
<script>
Vue.createApp(
{
data() {
return {
links: ['Home', 'About', 'Contact']
}
}
}
).mount('nav')
Vue.createApp(
{
data() {
return {
pageTitle: "Hello,Vue",
content: "Welcome to the wonderful world of Vue"
}
}
}
).mount('#app')
</script>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="link in links" class="nav-item">
<a class="nav-link" aria-current="page" :href="link.url"
:title="`this link goes to the ${link.text} page`">{{ link.text }}</a>
</li>
</ul>
</div>
</nav>
<div id="app" class="container">
<h1>{{ pageTitle }}</h1>
<p>{{ content }}</p>
</div>
<script>
Vue.createApp(
{
data() {
return {
links: [{ text: 'Home', url: 'home.html' },
{ text: 'About', url: 'about.html' },
{ text: 'Contact', url: 'contact.html' },
],
pageTitle: "Hello,Vue",
content: "Welcome to the wonderful world of Vue"
}
}
}
).mount('body')
</script>
</body>
</html>
因为没有编写页面,所以点击任意一个导航,都会出现
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="(page,index) in pages " class="nav-item">
<a class="nav-link" aria-current="page" :href="page.link.url"
:title="`this link goes to the ${page.link.text} page`" @click.prevent="activePage = index">
{{ page.link.text }}</a>
</li>
</ul>
</div>
</nav>
<div id="app" class="container">
<h1>{{ pages[activePage].pageTitle }}</h1>
<p>{{ pages[activePage].content }}</p>
</div>
<script>
Vue.createApp(
{
data() {
return {
activePage: 0,
pages: [
{
link: { text: 'Home', url: 'index.html' },
pageTitle: 'Home Page',
content: 'This is the home content'
},
{
link: { text: 'About', url: 'about.html' },
pageTitle: 'About Page',
content: 'This is the about content'
},
{
link: { text: 'Contact', url: 'contact.html' },
pageTitle: 'Contact Page',
content: 'This is the Contact content'
},
]
}
}
}
).mount('body')
</script>
</body>
</html>
使用 v-for 遍历 pages 数组中的每个页面,并将其相关信息绑定到导航栏链接。通过 @click.prevent 监听点击事件,当用户点击链接时,更新 activePage 的值为当前页面的索引,从而切换到对应的页面。prevent 修饰符用于防止点击事件的默认行为,例如阻止链接跳转或表单的提交。
@click.prevent=“activePage = index” 这行代码表示当用户点击导航栏中的链接时,将会触发一个点击事件,而 .prevent 修饰符阻止了这个点击事件的默认行为。在这个上下文中,默认行为通常是链接跳转。因此,点击链接时不会像通常一样跳转到链接指定的页面,而是触发了你在 @click 处理函数中定义的逻辑,即更新 activePage 的值为当前页面的索引,实现了页面切换的功能。
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg "
:class="{'navbar-light bg-light':!useDarkNavBar ,'navbar-dark bg-dark': useDarkNavBar }">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="(page,index) in pages " class="nav-item">
<a class="nav-link" aria-current="page" :href="page.link.url"
:title="`this link goes to the ${page.link.text} page`" @click.prevent="activePage = index">
{{ page.link.text }}</a>
</li>
</ul>
<form class="d-flex">
<button class="btn btn-primary" @click.prevent="useDarkNavBar = !useDarkNavBar">
Toggle NavBar
</button>
</form>
</div>
</nav>
<div id="app" class="container">
<h1>{{ pages[activePage].pageTitle }}</h1>
<p>{{ pages[activePage].content }}</p>
</div>
<script>
Vue.createApp(
{
data() {
return {
useDarkNavBar: false,
activePage: 0,
pages: [
{
link: { text: 'Home', url: 'index.html' },
pageTitle: 'Home Page',
content: 'This is the home content'
},
{
link: { text: 'About', url: 'about.html' },
pageTitle: 'About Page',
content: 'This is the about content'
},
{
link: { text: 'Contact', url: 'contact.html' },
pageTitle: 'Contact Page',
content: 'This is the Contact content'
},
]
}
}
}
).mount('body')
</script>
</body>
</html>
点击按钮,切换样式
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg " :class=navbarClasses>
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="(page,index) in pages " class="nav-item">
<a class="nav-link" aria-current="page" :href="page.link.url"
:title="`this link goes to the ${page.link.text} page`" @click.prevent="activePage = index">
{{ page.link.text }}</a>
</li>
</ul>
<form class="d-flex">
<button class="btn btn-primary" @click.prevent="useDarkNavBar = !useDarkNavBar">
Toggle NavBar
</button>
</form>
</div>
</nav>
<div id="app" class="container">
<h1>{{ pages[activePage].pageTitle }}</h1>
<p>{{ pages[activePage].content }}</p>
</div>
<script>
Vue.createApp(
{
computed: {
navbarClasses() {
return {
'navbar-light': !this.useDarkNavBar,
'bg-light': !this.useDarkNavBar,
'navbar-dark': this.useDarkNavBar,
'bg-dark': this.useDarkNavBar,
}
}
},
data() {
return {
useDarkNavBar: false,
activePage: 0,
pages: [
{
link: { text: 'Home', url: 'index.html' },
pageTitle: 'Home Page',
content: 'This is the home content'
},
{
link: { text: 'About', url: 'about.html' },
pageTitle: 'About Page',
content: 'This is the about content'
},
{
link: { text: 'Contact', url: 'contact.html' },
pageTitle: 'Contact Page',
content: 'This is the Contact content'
},
]
}
}
}
).mount('body')
</script>
</body>
</html>
Vue.js的computed属性来计算navbarClasses,这是一个基于useDarkNavBar数据属性的计算属性。computed属性允许你在Vue实例中定义依赖于其他响应式数据的属性,这些属性会在其依赖发生变化时自动更新。
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg " :class="[`navbar-${theme}`,`bg-${theme}`,'navbar','navbar-expand-lg']">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="(page,index) in pages " class="nav-item">
<a class="nav-link" aria-current="page" :href="page.link.url"
:title="`this link goes to the ${page.link.text} page`" @click.prevent="activePage = index">
{{ page.link.text }}</a>
</li>
</ul>
<form class="d-flex">
<button class="btn btn-primary" @click.prevent="changeTheme()">
Toggle NavBar
</button>
</form>
</div>
</nav>
<div id="app" class="container">
<h1>{{ pages[activePage].pageTitle }}</h1>
<p>{{ pages[activePage].content }}</p>
</div>
<script>
Vue.createApp(
{
data() {
return {
theme: 'light',
activePage: 0,
pages: [
{
link: { text: 'Home', url: 'index.html' },
pageTitle: 'Home Page',
content: 'This is the home content'
},
{
link: { text: 'About', url: 'about.html' },
pageTitle: 'About Page',
content: 'This is the about content'
},
{
link: { text: 'Contact', url: 'contact.html' },
pageTitle: 'Contact Page',
content: 'This is the Contact content'
},
]
};
},
methods: {
changeTheme() {
let theme = 'light';
if (this.theme == 'light') {
theme = 'dark';
}
this.theme = theme;
}
}
}
).mount('body')
</script>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 开发环境版本 -->
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg " :class="[`navbar-${theme}`,`bg-${theme}`,'navbar','navbar-expand-lg']">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="(page,index) in pages " class="nav-item">
<a class="nav-link" aria-current="page" :href="page.link.url"
:title="`this link goes to the ${page.link.text} page`" @click.prevent="activePage = index">
{{ page.link.text }}</a>
</li>
</ul>
<form class="d-flex">
<button class="btn btn-primary" @click.prevent="changeTheme()">
Toggle NavBar
</button>
</form>
</div>
</nav>
<page-viewer :page="pages[activePage]"></page-viewer>
<script>
let app = Vue.createApp(
{
data() {
return {
theme: 'light',
activePage: 0,
pages: [
{
link: { text: 'Home', url: 'index.html' },
pageTitle: 'Home Page',
content: 'This is the home content'
},
{
link: { text: 'About', url: 'about.html' },
pageTitle: 'About Page',
content: 'This is the about content'
},
{
link: { text: 'Contact', url: 'contact.html' },
pageTitle: 'Contact Page',
content: 'This is the Contact content'
},
]
};
},
methods: {
changeTheme() {
let theme = 'light';
if (this.theme == 'light') {
theme = 'dark';
}
this.theme = theme;
}
}
}
);
app.component('page-viewer', {
props: ['page'],
template: `
<div id="app" class="container">
<h1>{{page.pageTitle}}</h1>
<p>{{page.content}}</p>
</div> `
});
app.mount('body')
</script>
</body>
</html>
通过 app.component 方法创建了一个名为 page-viewer 的组件。这个组件接受一个名为 page 的 prop,并根据传入的页面信息渲染页面的标题和内容。
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Vue Basics</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://unpkg.com/vue@3"></script>
</head>
<body>
<navbar :pages="pages" :active-page="activePage" :nav-link-click="(index) => activePage = index"></navbar>
<page-viewer :page="pages[activePage]"></page-viewer>
<script>
const app = Vue.createApp({
data() {
return {
activePage: 0,
pages: [
{
link: { text: 'Home', url: 'index.html' },
pageTitle: 'Home Page',
content: 'This is the home content'
},
{
link: { text: 'About', url: 'about.html' },
pageTitle: 'About Page',
content: 'This is the about content'
},
{
link: { text: 'Contact', url: 'contact.html' },
pageTitle: 'Contact Page',
content: 'This is the Contact content'
}
]
};
}
});
app.component('page-viewer', {
props: ['page'],
template: `
<div id="app" class="container">
<h1>{{page.pageTitle}}</h1>
<p>{{page.content}}</p>
</div>
`
});
app.component('navbar', {
props: ['pages', 'activePage', 'navLinkClick'],
template: `
<nav class="navbar navbar-expand-lg"
:class="{ 'navbar-light': theme === 'light', 'navbar-dark': theme === 'dark', 'bg-light': theme === 'light', 'bg-dark': theme === 'dark' }">
<div class="container-fluid">
<a class="navbar-brand" href="#">My Vue</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li v-for="(page, index) in pages" class="nav-item">
<a class="nav-link" :href="page.link.url"
:title="[\`this link goes to the \${page.link.text} page\`]" @click.prevent="navLinkClick(index)">
{{ page.link.text }}
</a>
</li>
</ul>
<form class="d-flex">
<button class="btn btn-primary" @click.prevent="changeTheme()">
Toggle NavBar
</button>
</form>
</div>
</nav>
`,
data() {
return {
theme: 'light',
};
},
methods: {
changeTheme() {
this.theme = (this.theme === 'light') ? 'dark' : 'light';
}
}
});
app.mount('body');
</script>
</body>
</html>