Use javascript expressions: You can write simple expressions. (It can be a simple three-object operation, but it cannot be an if statement), and there will be computing attributes in the future.
The template replaces the mounted elements. The content of the mounted element is ignored. There is only one root node. The html structure is written in a script label and type = "x-template" is set ".
<body> <div id="box"> </div></body><script src="vue.js"></script><script type="text/javascript"> document.addEventListener('DOMContentLoaded',function () { var str = '
It indicates that the weight is relatively high. Directly "replace" the mount point and display the original html after replacement.
// Code snippet this is to use the script tag to internally define the template. Limitations: Cross-file use is not allowed, you can use <body> <div id = "box"> </div> </body> <script src = "vue. js "> </script> <script type =" x-template "id =" str "> <p> I am a p tag </p> </script> <script type = "text/javascript"> document. addEventListener ('domainloaded', function () {var vm = new Vue ({el: '# box', template:' # str'}) ;}, false ); </script>
Vue instance. Each application uses the Vue constructor to create a root instance to start New Vue (option object ). You need to input the option object, which contains elements, Data, templates, methods, and so on.
El: Mount element selector --- String | HtmlElement
Data: proxy data --- Object | Function
Methods: definition method --- Object
Vue proxy data, each vue instance will proxy all the properties in its data object, these properties are responded. The newly added attribute does not have the response function, and the view is not updated after the change.
The attributes and methods of a Vue instance expose its own attributes and methods, starting with "$", such as $ el and $ data...
Var vm = new Vue ({el: '# app', data: {message: 'Hello, Datura !!! '}, Methods: {test () {alert (1) ;}}, compontents: {// stores components here }}); /* vm is the new instance */console. log (vm. $ data); // that is, data. There are many attributes mounted on the vm (new) instance. // code snippets are placed in the template tag, and give an id <body> <template id = "tem"> <p> I am a template </p> </template> <div id = "box"> </ div> </body> <script src = "vue. js "> </script> <script type =" text/javascript "> document. addEventListener ('domainloaded', function () {var vm = new Vue ({el: '# box', template:' # tem '}) ;}, false ); </script>
3. template-render Function
The render function is very close to the editor.
Render => Option Object Attributes
Data Object Attributes
Class :{},=> bind class, and the same APIstyle :{},=> Binding style as v-bind: class, and the same APIattrs as v-bind: style: {},=> Add the line attribute domProps :{},=> DOM element attribute on :{},=> bind event nativeOn :{},=> listen to native event directives: {},=> custom command scopedSlots :{},=> slot scope slot :{},=> define the slot name and component relationship, insert Cao key: "key ", => Add a unique identifier for the element ref: "ref", => reference information 2Vue. js condition and loop Statement 2-1 Condition Statement v-if: the switching element will be destroyed and rebuilt based on the true and false values; => the element has disappeared in the dom v-show: based on the true and false values, switch the display attribute of the element; v-else: rendering when all conditions are inconsistent; v-else-if: Multi-condition judgment, rendering if true;
I. V-if
Use the v-if command for condition determination:
<Div id = "app"> <p v-if = "seen"> now you see me </p> <template v-if = "OK"> <p> hahaha, hard typing !!! </P> </template> </div> <script> new Vue ({el: '# app', data: {seen: true, OK: true }}) </script>
Here, the v-if command determines whether to insert the p element based on the value of the expression seen (true or false.
Ii. v-else
You can use the v-else command to add an "else" block to v-if:
Generate a random number, determine whether it is greater than 0.5, and then output the corresponding information:
<div id="app"> <div v-if="Math.random() > 0.5"> Sorry </div> <div v-else> Not sorry </div></div> <script>new Vue({ el: '#app'})</script>
Iii. v-show
We can also use the v-show command to display elements based on conditions:
<div id="app">
Iv. v-else-if
V-else-if is added in 2.1.0. As the name suggests, it is used as the else-if block of v-if. It can be chained for multiple times:
Judge the value of the type variable:
<div id="app"> <div v-if="type === 'A'"> A </div> <div v-else-if="type === 'B'"> B </div> <div v-else-if="type === 'C'"> C </div> <div v-else> Not A/B/C </div></div> <script>new Vue({ el: '#app', data: { type: 'C' }})</script>
[Use and comparison of v-show and v-if]
① V-show: Switch the display attribute of the element based on the true and false values;
The v-show element is always rendered and kept in the DOM.
V-show does not support the template syntax.
② V-if is a true conditional rendering, because it will ensure that the event listener and child components in the condition block are destroyed and rebuilt properly during the switching.
③ V-if has higher switching consumption and v-show has higher initial rendering consumption.
It is better to use v-show if frequent switchover is required. if the running conditions are unlikely to change, it is better to use v-if.
2-2 loop statement v-
① Syntax:v-for="x in items"
X is an index, and items is an array.
② The v-for loop is to constantly create new labels and the content in the labels. We decided that they are generally placed in an array and then displayed through traversal. You can also put objects and traverse objects.
③ When v-if and v-for are used together, v-for has a higher priority than v-if.
<Body> <div id = "app"> <ul> <li v-for = "(val, key) in fruitsArr ">{{ val }=>{{ key }}</li> // list items that are cyclically displayed </ul> </div> </body> <script src = ".. /vue. js "> </script> <script type =" text/javascript "> document. addEventListener ('domainloaded', function () {var vm = new Vue ({el: '# app', data: {fruitsArr: ['apple', 'bana ', 'orage', 'pear '] // data source}) ;}, false); </script>
Summary
The above is a detailed explanation of Vue. js usage introduced by the small editor. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!