Vue3 基础 P1
1.介绍 Vue3 核心用法
1.1 声明式渲染
先声明一个变量,然后渲染到DOM中。例如:
<div id="counter">
Counter:
</div>
const Counter = {
data() {
return {
counter: 0
}
}
}
Vue.createApp(Counter).mount('#counter')
使用 v-bind:
指令 绑定元素属性
<div id="bind-attribute">
<span v-bind:title="message">
鼠标悬停几秒钟查看此处动态绑定的提示信息!
</span>
</div>
const AttributeBinding = {
data() {
return {
message: 'You loaded this page on ' + new Date().toLocaleString()
}
}
}
Vue.createApp(AttributeBinding).mount('#bind-attribute')
1.2 处理用户输入
使用 v-on:
指令添加一个事件监听器
<div id="event-handling">
<p></p>
<button v-on:click="reverseMessage">反转 Message</button>
</div>
const EventHandling = {
data() {
return {
message: 'Hello Vue.js!'
}
},
methods: {
reverseMessage() {
this.message = this.message
.split('')
.reverse()
.join('')
}
}
}
Vue.createApp(EventHandling).mount('#event-handling')
使用 v-model=""
实现 表单输入 和 应用状态 之间的双向绑定。例如
<div id="two-way-binding">
<p></p>
<input v-model="message" />
</div>
const TwoWayBinding = {
data() {
return {
message: 'Hello Vue!'
}
}
}
Vue.createApp(TwoWayBinding).mount('#two-way-binding')
1.3 条件与循环
控制一个元素是否显示 使用 v-if=""
<div id="conditional-rendering">
<span v-if="seen">现在你看到我了</span>
</div>
const ConditionalRendering = {
data() {
return {
seen: true
}
}
}
Vue.createApp(ConditionalRendering).mount('#conditional-rendering')
使用 v-for
绑定数组的数据 渲染一个项目列表
<div id="list-rendering">
<ol>
<li v-for="todo in todos">
</li>
</ol>
</div>
const ListRendering = {
data() {
return {
todos: [
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
]
}
}
}
Vue.createApp(ListRendering).mount('#list-rendering')
1.4 组件化应用构建
组件系统是vue的一个重要概念,同时它是一个抽象概念。它允许我们使用小型,独立和可复用的组件 构建大型应用。
它把应用界面抽象成一个组件树。
在Vue 中,组件本质是一个具有预定义选项的实例。
在Vue中注册组件:
// 创建 Vue 应用
const app = Vue.createApp(...)
// 定义名为 todo-item 的新组件
app.component('todo-item', {
template: `<li>This is a todo</li>`
})
// 挂载 Vue 应用
app.mount(...)
把 todo-item
组件放到另一个组件的模板中:
<ol>
<!-- 创建一个 todo-item 组件实例 -->
<todo-item></todo-item>
</ol>
但是这样会为每个待办项渲染同样的文本,这看起来并不炫酷。
我们应该能将数据从父组件传入子组件才对。让我们来修改一下组件的定义,使之能够接受一个 prop:
app.component('todo-item', {
props: ['todo'],
template: `<li></li>`
})
现在使用 v-bind 指令将 代办项传到循环输出的 每个组件中:
<div id="todo-list-app">
<ol>
<!--
现在我们为每个 todo-item 提供 todo 对象
todo 对象是变量,即其内容可以是动态的。
我们也需要为每个组件提供一个“key”,稍后再
作详细解释。
-->
<todo-item
v-for="item in groceryList"
v-bind:todo="item"
v-bind:key="item.id"
></todo-item>
</ol>
</div>
const TodoList = {
data() {
return {
groceryList: [
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Whatever else humans are supposed to eat' }
]
}
}
}
const app = Vue.createApp(TodoList)
app.component('todo-item', {
props: ['todo'],
template: `<li></li>`
})
app.mount('#todo-list-app')
在大型应用中,有必要将整个应用程序划分成多个组件,以使开发更易管理。
例如一个假想的例子:组件的应用模板:
<div id="app">
<app-nav></app-nav>
<app-view>
<app-sidebar></app-sidebar>
<app-content></app-content>
</app-view>
</div>
2.1 创建一个应用实例
使用 createApp
函数 创建一个应用实例:
const app = Vue.createApp({
/* 选项 */
})
创建一个 全局
组件。例如:
const app = Vue.createApp({})
app.component('SearchInput', SearchInputComponent)
app.directive('focus', FocusDirective)
app.use(LocalPlugin)
允许链式调用。例如:
Vue.createApp({})
.component('SearchInput', SearchInputComponent)
.directive('focus', FocusDirective)
.use(LocalPlugin)
2.2 根组件
一个应用需要被挂载到DOM元素中。例如把一个Vue 应用挂载到 <div id="app"></div>
, 应该传入 #app
const RootComponent = {}
const app = Vue.createApp(RootComponet)
const vm = app.mount('#app')
大多数真实的应用都是被组织成嵌套的,可重用的组件树。例如,todo组件树可能是这样:
Root Component
└─ TodoList
├─ TodoItem
│ ├─ DeleteTodoButton
│ └─ EditTodoButton
└─ TodoListFooter
├─ ClearTodosButton
└─ TodoListStatistics
2.3 组件实例 property
const app = Vue.createApp({
data() {
return { count: 4 }
}
})
const vm = app.mount('#app')
console.log(vm.count) // => 4
其他组件选项:methods
, props
, computed
, inject
, setup
Vue 内置的 property: $attrs
, $emit
2.4 生命周期钩子
创建实例之后执行 created 钩子函数:
Vue.createApp({
data() {
return { count: 3 }
},
created() {
// this 指向 vm 实例
console.log(this.count) // => 3
}
})
其他钩子函数:mounted, updated, unmounted
不要在选项 property 或回调上使用箭头函数。比如:
created: () => console.log( this.a )
或
vm.$watch('a', newValue => this.myMethod() )
。因为箭头函数没有this
。
2.5 生命周期图示