This article takes you through the template syntax in Vue and introduces interpolation syntax and instruction syntax. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Vue has a lot of template syntax that is particularly easy to use. You can write some template syntax defined by Vue in HTML to quickly display data, binding methods, etc. This is one of the reasons why Vue is so quick to get started with.
Let’s first understand what is a template?
The template is a dynamic html page, which contains some js syntax codes
Vue’s template syntax is divided into two types, They are:
1. Interpolation syntax:
{{xxx}}
, xxx is a js expression, and all attributes in the data can be read directly, and the method of the object can be called. 2. Instruction syntax:
v-bind:href="xxx"
or abbreviated as :href ="xxx"
, xxx also needs to write js expressions, and can directly read all attributes in data v-????
[Related recommendation: "vue.js tutorial"]
Let’s introduce a few commonly used instructions. grammar.
v-bind:
Function: Specify the changed attribute value
Complete writing method
v-bind:xxx='yyy' // yyy会作为表达式解析执行
Concise writing method
:xxx='yyy'
Syntax: v-bind:href ="xxx"
or abbreviated as :href ="xxx"
Features: Data can only flow from data to page
v-model
Syntax: v-mode:value="xxx"
or abbreviated as v -model="xxx"
Features: Data can not only flow from data to page, but also from page to data
Function: Bind the callback function of the specified event nameComplete writing method
v-on:click='xxx' v-on:keyup='xxx(参数)' v-on:keyup.enter='xxx'
@click='xxx' @keyup='xxx' @keyup.enter='xxx'
v-text
v-text will replace the content in the node, but
{{xx}} will not.
v-html
will replace all the content in the node,
{{ xx}} will not.
can identify html structure.
v-htmlhas security issues! ! ! !
on trusted content, never on user-submitted content!
<body> <div id='app'> <h2>1. 大括号表达式</h2> <p>{{msg}}</p> <!--textContent --> <p>{{msg.toUpperCase()}}</p> <p v-html="msg"></p> <!--innerHTML --> <p v-text="msg"></p> <!--textContent --> <p v-text="msg.toUpperCase()"></p> <h2>2. 指令一: 强制数据绑定</h2> <img src="imgUrl" alt="Vue"> <!--无法显示图片,没有识别成js表达式 --> <img v-bind:src="imgUrl" alt="Vue"> <!--属性值识别成js表达式 --> <img :src="imgUrl" alt="Vue"> <h2>3. 指令二: 绑定事件监听</h2> <button v-on:click="test1">test1</button> <button @click="test1">test1</button> <button @click="test2('abc')">test2</button> <!--可以传参数 --> <button @click="test2(msg)">test2</button> </div> <script src="../js/vue.js"></script> <script> new Vue({ el: '#app', data: { msg: '<a href="http:www.baidu.com">I Will Back!</a>', imgUrl: "https://cn.vuejs.org/images/logo.png" }, methods: { test1() { alert('heheh'); }, test2(content){ alert(content); } } }) </script> </body>
Remove tag delete##v -if
##v-else="expression"
Applies to: Toggle Less frequent scenarios.
Features: DOM elements that are not displayed are removed directly.
Note: v-if can be used together with: v-else-if and v-else, but the structure must not be "interrupted". Add style hiding (display: none)
v-show
[Remarks] When using v-if, the element may not be obtained, but it can definitely be obtained using v-show.
v-if
是控制元素是否加载到页面上(有性能开销) v-show
是控制元素的显示与隐藏 (初始创建时加载一次)
<body> <div id="demo"> <p v-if="ok">成功了</p> <!-- 移除标签删除 --> <p v-else>失败了</p> <p v-show="ok">又成功了</p> <!-- 添加样式隐藏 --> <p v-show="!ok">又失败了</p> <button @click="ok = !ok">切换</button> </div> <script src="../js/vue.js"></script> <script> new Vue({ el: '#demo', data: { ok: false, } }) </script> </body>
一些常用的指令
v-text
: 更新元素的 textContentv-html
: 更新元素的 innerHTMLv-if
: 如果为true, 当前标签才会输出到页面v-else
: 如果为false, 当前标签才会输出到页面v-show
: 通过控制display样式来控制显示/隐藏v-for
: 遍历数组/对象v-on
: 绑定事件监听, 一般简写为@v-bind
: 强制绑定解析表达式, 可以省略v-bindv-model
: 双向数据绑定ref
: 为某个元素注册一个唯一标识, vue对象通过$refs属性访问这个元素对象v-cloak
: 使用它防止闪现表达式, 与css配合: [v-cloak] { display: none }更多编程相关知识,请访问:编程入门!!
The above is the detailed content of An in-depth analysis of template syntax in Vue: interpolation and directives. For more information, please follow other related articles on the PHP Chinese website!