- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- template:`<div>hello worlddiv>`
- })
- app.mount("#root")
- script>
- html>
在 “{{}}”中可以写入一行表达式,可以直接获取data的数据。
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data(){
- return{
- message:'hello world'
- }
- },
- template:`{{message}}`
- })
- app.mount("#root")
- script>
- html>
- <script>
- const app = Vue.createApp({
- data(){
- return{
- message:'hello world'
- }
- },
- template:`{{message}}`
- })
- app.mount("#root")
- script>
如果单纯插值表达式,会将 html 标签原样输出
此刻使用 v-html 可以解决这个问题,v-html 指令是设置 innerHTML
- <script>
- const app = Vue.createApp({
- data(){
- return{
- message:'hello world'
- }
- },
- template:``
- })
- app.mount("#root")
- script>
使用 [attributeName] 即可插值属性名
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
-
- data(){
- return{
- name:'title',
- message:'dell'
- }
- },
- template:`
- <div v-bind:[name]="message">div>
- `
- })
- app.mount("#root")
- script>
- html>
Vue中有2种数据绑定的方式:
单向绑定(v-bind):数据只能从data流向页面
双向绑定(v-model):数据不仅能从data流向页面,还可以从页面流向data
备注:
双向绑定一般都应用在表单类元素上(如:、
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
-
- data(){
- return{
- mes11:"uuu"
- }
- },
- template:`
- <input type="text" v-bind:value="mes11"/>
- <input type="text" v-model="mes11"/>
- `
- })
- app.mount("#root")
- script>
- html>
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data:function(){
- return({
- msg:[]
- })
- },
- template: `
- {{msg}}
- jack <input type="checkbox" v-model="msg" value="jack" />
- dell <input type="checkbox" v-model="msg" value="dell" />
- lee <input type="checkbox" v-model="msg" value="lee" />
- `
- })
- app.mount("#root")
- script>
-
- html>
使用数组类型可以获得 checkbox的合集
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data:function(){
- return({
- msg:'A'
- })
- },
- template: `
- {{msg}}
- <select v-model="msg">
- <option>Aoption>
- <option>Boption>
- <option>Coption>
- select>
- `
- })
- app.mount("#root")
- script>
-
- html>
computed是vue的计算属性,是根据依赖关系进行缓存的计算,只有在它的相关依赖发生改变时才会进行更新。
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
-
- data(){
- return{
- message:"hello world",
- count:2,
- price:5
- }
- },
- computed:{
- total(){
- return this.count * this.price
- }
- },
- template:`
- {{total}}
- `
- })
- app.mount("#root")
- script>
- html>
结果输出 10
watch对象中,属性名为侦听的数据名,当数据被修改即可调用这个函数。
computed 和 watch 都能实现的功能建议使用conputed实现,更加简洁而且有缓存。
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
-
- data(){
- return{
- msga:"hello"
- }
- },
- watch:{
- msga(){
- setTimeout(()=>{
- console.log(111)
- },1000)
- }
- },
- template:`
-
- `
- })
- app.mount("#root")
- script>
- html>
v-on:click 等价于 @click 点击后触发事件
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data() {
- return {
- counter: "1"
- }
- },
- methods:{
- handleBtnClick:function(event){
- console.log(event.target)
- }
- },
- template: `
- <div @click="handleBtnClick">{{counter}}div>
- `
- })
- app.mount("#root")
- script>
-
- html>
事件绑定可以传递参数,$event 可以传递真实的dom
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data() {
- return {
- counter: "1"
- }
- },
- methods:{
- handleBtnClick:function(num,event){
- console.log(event)
- }
- },
- template: `
- {{counter}}
- `
- })
- app.mount("#root")
- script>
-
- html>
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data() {
- return {
- counter: "1"
- }
- },
- methods:{
- handleDivClick:function(){
- console.log('dev')
- },
- handleBtnClick:function(){
- console.log('button')
- }
- },
- template: `
-
-
-
- `
- })
- app.mount("#root")
- script>
-
- html>
如果事件嵌套事件,会冒泡触发,内部的实现会触发外部事件。
对外层元素使用 .self 即可阻止冒泡
- <div @click.self="handleDivClick">
- <button @click="handleBtnClick">buttonbutton>
- div>
事件修饰符还有 .prevent 阻止默认行为,.capture 捕获模式,.once 只执行一次等...
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- methods:{
- handleKeyDown:function(){
- console.log(1)
- }
- },
- template: `
-
- `
- })
- app.mount("#root")
- script>
-
- html>
当进行指定的按键操作即可触发函数
常用有按键修饰符 enter tab delete esc up down left right
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data:function(){
- return({
- show:false
- })
- },
- template: `
- hello world
- bye world
- `
- })
- app.mount("#root")
- script>
-
- html>
v-if 直接不渲染这个dom元素,v-show采用display:none的方式
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data:function(){
- return({
- list:['dell','les','teacher']
- })
- },
- template: `
-
-
- {{value}}
-
- `
- })
- app.mount("#root")
- script>
-
- html>
效果如下:
还可以使用 value key 的形式输出
<li v-for="(value,key) in list">{{value}}--{{key}}li>
- html>
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>hello worldtitle>
- <script src="https://unpkg.com/vue@next">script>
- head>
-
- <body>
- <div id="root">div>
- body>
- <script>
- const app = Vue.createApp({
- data:function(){
- return({
- listObject:{
- firstName:'dell',
- lastName:'lee',
- job:'teacher'
- }
- })
- },
- template: `
-
-
- {{value}}---{{key}}---{{index}}
-
- `
- })
- app.mount("#root")
- script>
-
- html>
如果 v-for 一个对象 可以获得 value,key,index 三个值