本教程操作环境:windows7系统、vue2.9.6版,dell g3电脑。
vue中的6种插值操作第一种:mustache
mustache语法(也就是双大括号)。mustache: 胡子/胡须。(胡子语法)
数据是响应式的
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <script src="vue.js"></script></head><body> <div id="app"> <h2>{{message}},world!</h2> <h2>{{counter * 2}}</h2> <h2>{{message}} {{counter}}</h2> </div> <script> let app = new vue({ el: '#app', data: { message: 'hello', counter:200 }, methods: { } }) </script></body></html>
第二种:v-once
该指令后面不需要跟任何表达式(比如之前的v-for后面是由跟表达式的)该指令表示元素和组件只渲染一次,不会随着数据的改变而改变。
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <script src="vue.js"></script></head><body> <div id="app"> <h2>{{message}}</h2> <h2 v-once>{{message}}</h2> </div> <script> let app = new vue({ el: '#app', data: { message: 'hello' } }) </script></body></html>
第三种:v-html
从服务器请求到的数据本身就是一个html代码
该指令后面往往会跟上一个string类型,会将string的html解析出来并且进行渲染
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <script src="vue.js"></script></head><body> <div id="app"> <h2>{{link}}</h2> <h2 v-html>{{link}}</h2> <h2 v-html="link"></h2> </div> <script> let app = new vue({ el: '#app', data: { link: '<a href="http://www.baidu.com">百度一下</a>' } }) </script></body></html>
第四种:v-text
v-text作用和mustache比较相似:都是用于将数据显示在界面中v-text通常情况下,接受一个string类型
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <script src="vue.js"></script></head><body> <div id="app"> <h2>{{message}}</h2> <h2 v-text="message2"></h2> <h2 v-text="message2">{{message}}</h2> </div> <script> let app = new vue({ el: '#app', data: { message: 'hello', message2:'world' } }) </script></body></html>
第五种:v-pre
v-pre用于跳过这个元素和它子元素的编译过程,用于显示原本的mustache语法。
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <script src="vue.js"></script></head><body> <div id="app"> <h2>{{message}}</h2> <h2 v-pre>{{message}}</h2> </div> <script> let app=new vue({ el:'#app', data:{ message:'hello' } }) </script></body></html>
第六种:v-cloak
可能会直接显然出未编译的mustache标签cloak: 斗篷
<!doctype html><html><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <script src="vue.js"></script> <style> [v-cloak]{ display: none; } </style></head><body> <div id="app"> <h2>hello,{{name}}</h2> <h2 v-cloak>hello,{{name}}</h2> </div> <script> settimeout(()=>{ let app=new vue({ el:'#app', data:{ name:'world' } }) },10000) </script></body></html>
(学习视频分享:vuejs教程、web前端)
以上就是vue插值有哪些操作的详细内容。
