vue简单实现
Vue 简单实现步骤
安装 Vue.js 最新版本:
npm install vue@latest
创建 Vue 实例:
import { createApp } from 'vue'
const app = createApp({
data() {
return {
message: 'Hello Vue!'
}
}
})
挂载到 DOM:
app.mount('#app')
基础模板语法
显示数据:
<div id="app">
{{ message }}
</div>
绑定属性:
<div v-bind:id="dynamicId"></div>
<!-- 简写 -->
<div :id="dynamicId"></div>
条件渲染
使用 v-if:
<p v-if="seen">现在你看到我了</p>
<p v-else>现在你看不到我了</p>
列表渲染
使用 v-for:
<ul>
<li v-for="item in items" :key="item.id">
{{ item.text }}
</li>
</ul>
事件处理
使用 v-on:
<button v-on:click="reverseMessage">反转消息</button>
<!-- 简写 -->
<button @click="reverseMessage">反转消息</button>
表单输入绑定
使用 v-model:
<input v-model="message" placeholder="编辑我">
<p>输入的是:{{ message }}</p>
组件基础
注册组件:
app.component('todo-item', {
props: ['todo'],
template: `<li>{{ todo.text }}</li>`
})
使用组件:
<ol>
<todo-item
v-for="item in groceryList"
:todo="item"
:key="item.id"
></todo-item>
</ol>






