概述
创建 vue 实例,初始化渲染
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<body>
<div id="app">
<!--渲染的逻辑代码-->
</div>
</body>
<script>
const app = new Vue({
// 通过 el 配置器,指定 Vue 管理的盒子
el:'#app',
// data 提供谁
data:{
message:'Hello Vue'
}
})
</script>
插值表达式
是一种 Vue 的模板语法
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<body>
<div id="app">
<h1></h1>
</div>
</body>
<script>
const app = new Vue({
el:'#app',
data:{
name: 'haonan',
data: 18,
}
})
</script>
响应式特性
数据改变,视图会自动更新
聚焦于数据 -> 数据驱动视图(就是使用 Vue 开发,只关注业务的核心逻辑,根据业务修改数据即可)
开发者工具:插件调试 Vue 引用
Https://chrome.zzzmh.cn/index
概述
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<body>
<div id="app">
<div v-html="message"></div>
</div>
</body>
<script>
const app = new Vue({
el: '#app',
data: {
message: 'Hello1'
}
})
</script>
v-show
v-if
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<body>
<div id="app">
<div v-show="flag">v-show 控制的盒子</div>
<div v-if="flag">v-if 控制的盒子</div>
</div>
</body>
<script>
const app = new Vue({
el: '#app',
data: {
flag:false
}
})
</script>
v-else、v-else-if
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<body>
<div id="app">
<p v-if="gender === 1">性别:男</p>
<p v-else>性别:女</p>
<hr>
<p v-if="score >= 90">成绩评定A:奖励电脑一台</p>
<p v-else-if="score >= 70">成绩评定B:奖励周末打游戏</p>
<p v-else-if="score >= 60">成绩评定C:奖励零食</p>
<p v-else>成绩评定D:奖励一大比兜</p>
</div>
</body>
<script>
const app = new Vue({
el: '#app',
data:{
gender: 1,
score: 80
}
})
</script>
v-on
<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
<body>
<div id="app">
<button v-on:click="count++">+</button>
<span></span>
<!-- 简写-->
<button @click="count--">-</button>
</div>
</body>
<script>
const app = new Vue({
el: '#app',
data: {
count: 100
}
})
</script>
<body>
<div id="app">
<div class="box">
<h3>小黑自动售货机</h3>
<button @click="buy(5)">可乐5元</button>
<button @click="buy(10)">咖啡10元</button>
</div>
<p>银行卡余额:元</p>
</div>
</body>
<script>
const app = new Vue({
el: '#app',
data: {
money: 100
},
methods: {
buy(price) {
this.money -= price
}
}
})
</script>
v-bind
示例
<script src="./vue.js"></script>
<body>
<div id="app">
<button v-show="index > 0" @click="index--">上一页</button>
<div>
<img :src="list[index]" alt="">
</div>
<button v-show="index < list.length - 1" @click="index++">下一页</button>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
list: [
'./imgs/11-00.gif',
'./imgs/11-01.gif',
'./imgs/11-02.gif',
'./imgs/11-03.gif',
'./imgs/11-04.png',
'./imgs/11-05.png',
],
index: 0
},
})
</script>
</body>
v-for
<script src="./vue.js"></script>
<body>
<div id="app">
<h3>小黑水果店</h3>
<ul>
<li v-for="(item,index) in list" :key="item.id"> -- </li>
</ul>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
list: ['西瓜', '苹果', '鸭梨']
}
})
</script>
</body>
案例:
<div id="app">
<h3>小黑的书架</h3>
<ul>
<li v-for="item in booksList">
<span></span>
<span></span>
<button @click="del(item.id)">删除</button>
</li>
</ul>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
booksList: [
{id: 1, name: '《红楼梦》', author: '曹雪芹'},
{id: 2, name: '《西游记》', author: '吴承恩'},
{id: 3, name: '《水浒传》', author: '施耐庵'},
{id: 4, name: '《三国演义》', author: '罗贯中'}
]
},
methods: {
del(id) {
this.booksList = this.booksList.filter(item => item.id != id)
}
}
})
</script>
</body>
v-for 的默认行为会尝试原地修改元素(就地复用)
v-model
<script src="./vue.js"></script>
<body>
<div id="app">
账户:<input type="text" v-model="username"> <br><br>
密码:<input type="password" v-model="password"> <br><br>
<button @click="login">登录</button>
<button @click="reset">重置</button>
</div>
<script>
const app = new Vue({
el: '#app',
data: {
username: '',
password: '',
},
methods: {
login() {
console.log(this.username, this.password)
},
reset() {
this.username = ""
this.password = ""
}
}
})
</script>
</body>
功能需求
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<body>
<!-- 主体区域 -->
<section id="app">
<!-- 输入框 -->
<header class="header">
<h1>小黑记事本</h1>
<input placeholder="请输入任务" class="new-todo" v-model="content"/>
<button class="add" @click="add">添加任务</button>
</header>
<!-- 列表区域 -->
<section class="main">
<ul class="todo-list">
<li class="todo" v-for="(item,index) in list" :key="item.id">
<div class="view">
<span class="index">NaN.</span> <label></label>
<button class="destroy" @click="del(item.id)"></button>
</div>
</li>
</ul>
</section>
<!-- 统计和清空 -->
<footer class="footer" v-show="list.length > 0">
<!-- 统计 -->
<span class="todo-count">合 计:<strong> </strong></span>
<!-- 清空 -->
<button class="clear-completed" @click="clear">
清空任务
</button>
</footer>
</section>
<!-- 底部 -->
<script>
const app = new Vue({
el: '#app',
data: {
list: [
{id: 1, name: "起床"},
{id: 2, name: "上班"},
{id: 3, name: "午睡"},
],
content: ""
},
methods: {
del(id){
this.list = this.list.filter(item => item.id != id)
},
add(){
if(this.content.trim() === ''){
alert("未输入要添加的内容")
}
this.list.push({id: this.list.length + 1,name: this.content})
this.content = ""
},
clear(){
this.list = []
}
}
})
</script>
</body>