更新元素的 innerHTML。
注意:内容按普通 HTML 插入 - 不会作为 Vue 模板进行编译。
示例:
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
{{myhtml}}
<div v-html="myhtml">在 v-html 里面设置内容无效div>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
myhtml: '1111111'
},
})
script>
body>
html>
textContent
。如果要更新部分的 textContent
,需要使用 {{ Mustache }}
插值。<span v-text="msg">span>
<span>{{msg}}span>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
<span v-text="msg">在 v-text 里面设置内容无效span>
<br />
<span>我是:{{msg}}span>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
msg: "textContent"
},
})
script>
body>
html>
display: none
属性。DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
<p v-if="isShow">v-if控制显隐p>
<p v-show="isShow">v-show控制显隐p>
<button @click="handleClick">切换button>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
isShow: true
},
methods: {
handleClick() {
this.isShow = !this.isShow
}
}
})
script>
body>
html>
销毁-重建
的过程,而不是设置CSS样式display:none
控制的DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
<span v-if="value < 0">value 小于 0span>
<span v-else-if="value === 0">value 等于 0span>
<span v-else>value 大于 0span>
<button @click="handleClick">切换button>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el:"#app",
data:{
value:-1
},
methods:{
handleClick(){
// 实现 3 个 span 标签来回切换
this.value > 0 ?this.value = -1:++this.value
}
}
})
script>
body>
html>
alias in expression
<div v-for="item in items">div>
<div v-for="(item, index) in items">div>
<div v-for="(val, key) in object">div>
<div v-for="(val, name, index) in object">div>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
<ul>
<li v-for="item in dataArray">item = {{item}}li>
<li v-for="(item,index) in dataArray">item = {{item}}、index = {{index}}li>
<li v-for="(val, key) in dataObject">val = {{val}}、key = {{key}}li>
<li v-for="(val, name, index) in dataObject">val = {{val}}、name = {{name}}、index = {{index}}li>
ul>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el:"#app",
data:{
dataArray:['aa','bb','cc'],
dataObject:{name: "jj",age: 18}
}
})
script>
body>
html>
@
v-on
简写就是@
。.stop // 调用 event.stopPropagation()。
.prevent // 调用 event.preventDefault()。
.capture // 添加事件侦听器时使用 capture 模式。
.self // 只当事件是从侦听器绑定的元素本身触发时才触发回调。
.{keyCode | keyAlias} //只当事件是从特定键触发时才触发回调。
.native // 监听组件根元素的原生事件。
.once // 只触发一次回调。
.left // 只当点击鼠标左键时触发。
.right // 只当点击鼠标右键时触发。
.middle // 只当点击鼠标中键时触发。
.passive // 模式添加侦听器
<button v-on:click="doThis">button>
<button v-on:[event]="doThis">button>
<button v-on:click="doThat('hello', $event)">button>
<button @click="doThis">button>
<button @[event]="doThis">button>
<button @click.stop="doThis">button>
<button @click.prevent="doThis">button>
<form @submit.prevent>form>
<button @click.stop.prevent="doThis">button>
<input @keyup.enter="onEnter">
<input @keyup.13="onEnter">
<button v-on:click.once="doThis">button>
<button v-on="{ mousedown: doThis, mouseup: doThat }">button>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
v-on用来绑定事件:
<button v-on:click="num++">++button>{{num}}<br>
v-on 可以简写成@:
<button @click="num--">--button>{{num}}<br>
vue的属性表达式中不可以直接写业务逻辑<br>
<button v-on:click="alert(666)">Vue-alert-报错button>
<button onclick="alert(777)">js-alert-正确button><br>
事件绑定,调用函数<br>
<button v-on:click="show1">调用无参函数button>
<button v-on:click="show2(5,10)">调用传参函数button>
<button v-on:click="show3(5,10,$event)">调用传$event函数button><br>
可以使用 $event提供的属性 接收 文本框的内容,$event是一个事件对象,里面有很多属性可以操作<br>
<input type="text" value="" v-on:input="show4($event)" />{{mytext}}<br>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
num: 0,
mytext: ''
},
// 所有的自定义函数都写在这里
methods: {
show1(ev) { // 接收一个未定义的参数相当于:$event
console.log("无参函数被调用了,接收一个未定义的参数时:");
console.log(ev);
},
show2(a, b) {
console.log('传普通参的函数被调用了:' + (a + b));
},
show3(a, b, ev) {
console.log('传$event参数的函数被调用了:');
console.log(ev);
},
show4(ev) {
console.log(ev.target.value);
this.mytext = ev.target.value;
}
}
})
script>
body>
html>
:
v-bind
的缩写为 :
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<style>
.red {
background-color: red;
}
.green {
background-color: green;
}
style>
<body>
<div id="app">
<p class="red">设置背景色p>
<p class="isActive ? 'red':'green'">动态切换背景色p>
<p v-bind:class="isActive ? 'red':'green'"> v-bind 动态切换背景色p>
<p :class="isActive ? 'red':'green'"> : 动态切换背景色p>
<button @click="handleClick">切换button>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
isActive: true
},
methods: {
handleClick() {
this.isActive = !this.isActive
}
}
})
script>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<style>
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.yellow {
background-color: yellow;
}
style>
<body>
<div id="app">
<p :class="classObj">class对象写法p>
<p :class="classArr">class数组写法p>
<hr>
<p :style="styleObj">style对象写法p>
<p :style="styleArr">style数组写法p>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
classObj: {
red: true,
green: true,
blue: true
},
styleObj: {
backgroundColor: 'red',
},
classArr: ['red', 'green', 'blue'],
styleArr: [{
backgroundColor: 'red'
}, ]
},
mounted() {
this.classArr.push('yj') // 向数组中追加属性
this.styleArr.push({
fontSize: '20px'
}) // 向数组中追加属性
}
})
Vue.set(app.classObj, 'yellow', true) // 向对象中追加属性
Vue.set(app.styleObj, 'fontSize', '40px') // 向对象中追加属性
script>
body>
html>
在表单控件或者组件上创建双向绑定
只能作用于: 或
或
或组件
修饰符有:
.lazy
:取代 input 监听 change 事件.number
:输入字符串转为有效的数字.trim
: 输入首尾空格过滤例如:
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
<h1>注册表单h1>
<div>用户名:
<input type="text" value="" name="username" v-model="username" />{{username}}
div>
<div>密码:
<input type="password" value="" name="pwd" v-model="pwd" />{{pwd}}
div>
<div>
性别:
<input type="radio" value="1" name="sex" v-model="sex" />男
<input type="radio" value="0" name="sex" v-model="sex" />女
{{sex}}
div>
<div>
爱好:
<input type="checkbox" name="hobby" value="篮球" v-model="hobby" />篮球
<input type="checkbox" name="hobby" value="足球" v-model="hobby" />足球
<input type="checkbox" name="hobby" value="乒乓" v-model="hobby" />乒乓
{{hobby}}
div>
<div>
阶段:
<select v-model="lesson" name="lesson">
<option value="1">1阶段option>
<option value="2">2阶段option>
<option value="3">3阶段option>
select>
{{lesson}}
div>
<div>
来自哪个城市:
<select v-model="city" name="city">
<option value="" disabled>未选中option>
<option v-for="i in cityList" :value="i.id">{{i.name}}option>
select>
{{city}}
div>
<div>
<input type="checkbox" v-model="flag" name="flag" /> 已阅读***,并同意!{{flag}}
div>
<div>
<button v-on:click="mySubmit">注册button>
div>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
username: "",
pwd: "",
sex: "",
hobby: [],
lesson: 3,
cityList: [{
"id": 1,
"name": "深圳"
},
{
"id": 2,
"name": "北京"
},
{
"id": 3,
"name": "上海"
}
],
city: "",
flag: false
},
// vue所有的自定义函数都放在这里
methods: {
mySubmit: function() {
console.log("注册已完成");
console.log({
"username": this.username,
"hobby": this.hobby
});
}
}
})
script>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
{{msg}}
<hr>
<div v-once>{{num}}div>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
"msg": "hello",
"num": 10
},
mounted() {
setTimeout(() => {
this.msg = 'wold';
this.num = 12;
}, 2000)
}
})
script>
body>
html>
使用 vue 开发时,在 vue 初始化之前,由于 div 是不归 vue 管的,所以我们写的代码在还没有解析的情况下会容易出现花屏现象,看到类似于
{{message}}
的字样,虽然一般情况下这个时间很短暂,但是我们还是有必要让解决这个问题的
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<style>
[v-cloak] {
display: none;
}
style>
<body>
<div id="app">
<div>{{msg}}div>
<hr>
<div v-cloak>{{msg}}div>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
msg: '注意看我是否闪动了'
},
})
script>
body>
html>
DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title>
head>
<body>
<div id="app">
<p >{{ msg }}p>
<p v-pre>{{ msg }}p>
div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
<script>
var app = new Vue({
el: "#app",
data: {
msg: '一段要被编译的文本'
},
})
script>
body>
html>
#
#
<base-layout>
<template v-slot:header>
Header content
template>
Default slot content
<template v-slot:footer>
Footer content
template>
base-layout>
<infinite-scroll>
<template v-slot:item="slotProps">
<div class="item">
{{ slotProps.item.text }}
div>
template>
infinite-scroll>
<mouse-position v-slot="{ x, y }">
Mouse position: {{ x }}, {{ y }}
mouse-position>