Exploring Vue.js Component Content Realization _javascript Skill

Source: Internet
Author: User

Now take a systematic look at Vue (refer to Vue.js official documentation):

Vue.js is a library that constructs a data-driven Web interface with the goal of implementing the response data binding and combining the attempted components .

Vue.js embraces data-driven view concepts, which means that we can "bind" the DOM to the underlying data in ordinary HTML templates using special usage. Once the bindings are created, the DOM remains synchronized with the data.

The following reference code corresponds to the model above

<!--This is our View-->
<div id= "example-1" >
   Hello {{name}}!
</div>/
/This is our Model
var exampledata = {
   name: ' Vue.js '
}
 
///Create a Vue instance or ' ViewModel '
It connects View with Model
var examplevm = new Vue ({
   el: ' #example-1 ',  //mount on an entity with ID ' example-1 ')
   Data:exam Pledata  //Data Flow
})

Usually we will write the model in the Vue instance, the following wording is the same as the above:

<!--This is our View-->
<div id= "example-1" >
   Hello {{name}}!  <!---Vue data model is wrapped with {{Datamodel}}--->
</div>
 
//Create a Vue instance or "ViewModel"
//It connects View with model< Br><script>
var examplevm = new Vue ({
   el: ' #example-1 ',  //on an entity with ID ' example-1 ' to mount
   data: { C11/>name: ' Vue.js '
      }//Data Flow
} <br></script>

After such a program is executed, the ' Hello vue.js! ' is displayed in the #example-1 control.

Here's a look at the Instructions (directives):
Directives are special with a prefix-V attribute, and the value of the instruction is limited to a binding expression, such as an IF instruction:

<p v-if= "Greeting" >hello!<p>

There are also binding directives that bind some property values to some values, such as:

<input:type = "Questitem.type",: Name = "Questitem.name"/>

This omits "V-bind", making the value of the property value of input have the effect of "calculation".

Calculation properties

Here is an introduction to the use of $watch, which is used when a data needs to be changed according to other data:

<script><br>var vm = new Vue ({
 el: ' #demo ',
 data: {
  firstName: ' Foo ',
  lastName: ' Bar ',
  fullName: ' Foo Bar '
 }
} <br></script>
 
vm. $watch (' FirstName ', function (val) {//When FirstName changes, immediately update Vm.fullname
 This.fullname = val + ' + this.lastname
} '
 
vm. $watch (' LastName ', function (val) {  //when LastName changes, immediately update VM . FullName
 this.fullname = this.firstname + ' + val
} ')

Here, all the data objects can be accessed through vm.firstname.

V-model

As the name suggests, it is the data model in Vue, which is used to bind the data in the Vue instance:

<!---bi-direction bound--->
<div id= "app" >
  <p>{{message}}</p>
  <input v-model= "Message" > <!--model,input input data is immediately fed back to the Vue instance--> 
</div>
<script>
  New Vue ({
    el: ' #app ',  //View
    data: {message
      : ' Hello vue.js '
    }
  })
</script>

For example, to bind a form control, you display the selected value:

<!---    form control bindings-Radio--->
<div id= "Myselect" >  //Outside this layer I didn't add at first, and then it went wrong, El seems to be generally mounted on the <div> component
<select v-model= "selected" >//Data type is selected, the value is the selected value
  <option
  <option>B</option>
  <option>C</option>
</select>
<span> Selected: {{Selected}}</span>
</div>
 
<script>
  new Vue ({
    el: ' #myselect ',
    data:{
      selected:[]
    }
  )
</script>

V-if, V-else

This command can be used very flexible, such as I generated a new topic in the form, there are "single choice", "multiple-choice", "text question" three kinds, then for different topics should be displayed on the control, you can use the following syntax:

<div v-if= "Questitem.type = = ' textarea '" >//note is three equals sign
    <textarea></textarea>
</div >
<div v=else>
    <div></div>
</div>

V-for

This is used for traversal of array elements, for example:

<ul id= "Demo" >
  <li
    v-for= "item in Items"
    class= "item-{{$index}}" >  <!---$ Index refers to the position of the current array element in the array---> {{parentmessage}}}-{{
    $index}}-{{item.message}}} <!--A view structure-->
  </ li>
</ul>
<button id= "BTN1" > Click me </button>
<script>
  var demo= new Vue ({
    el: ' #demo ',
    data:{
      parentmessage: ' Parent ',
      items:[
        {message: ' Foo '},
        {message: ' Bar '}
      }
  })
</script>

The above code means traversing the items array in the demo instance, displaying each of the elements (' Foo ', ' Bar ') in the <li> tab respectively.

To avoid rendering the entire list, it is often used: track-by = "$index", which means that only the current array element is manipulated.

At this point, about Vue the most basic things have been introduced, need more API information can refer to: http://cn.vuejs.org/api/

The structure of VUE files and the control of data flow

In the Vue file, we can often see such a format:

<template> <div> </div> </template> <script> export default{data () {...
          methods:{///custom method that can be processed on data method1 () {...}
       ...}, components: {...}
          Vuex: {getters:{//Get Store data questionnaire:state => State.currentquestionnaire} Actions: {///used to distribute data containers store mutations Method Action1 () {Dispatch ("Set_quest", Item)}//Dispatch used to invoke the parent component Stor
       E's "Set_quest" Method Action2 () {...}
            } directives: {' my-directive ': {bind:function () {//hook function, called only once, when the instruction is first bound to the element} Update:function (NewValue, OldValue) {//Hook function, the first call after bind with the initial value, and then every time the binding to the change is invoked} unbind:function () {//Hook function    
     , called only once, when the instruction is unbound from the element, call}}//custom directive, v-my-directives in <template> with <div = "" > method
 } </script> <style> </style>

<template> is placed in the control that is owned by this page (or part of the page), while <script> defines Vue data objects and methods,<style> the CSS style of the control.

The "This" keyword is frequently used in methods, which points to Vue component instances.

Event.target: The concrete control that triggers the event, does not produce bubbles, who is who, this is often used when locking event-triggered controls, such as:

<div @click. Stop = "AddItem ($event)" >
   <span data-type = "Radio" > Single selection </span>
   <span Data-type = "checkbox" > Multiple selection </span>
   <span data-type = "textarea" > Text title </span>
</div>
 
<script>
   Export default{method
      : {
          AddItem (event) {let
             target = Event.target
             if ( Target.nodeName.toLowerCase () = = ' span ') {//When clicked on the selected button
                This.showmask = True  //Show pop-up box
                This.questItem.type = Target.dataset.type  //set type of Problem}}
</script>

Finally, talk about this. $els: An object that contains a DOM element registered with V-el

<div class = "Promt-header" > <div> <label> Problem Name: </label> <input type = "text", pl Aceholder = "Please enter title" V-el:item-title/> </div> </div> <div class = "Promt-footer" @click. Stop = "Handle Input (&event) "> <button type =" button "data-operation =" confirm "> OK </button> <button type = "button" Data-operation = "Cancel" > Cancel </button> </div> <script> methods: {Handleinpu T (event) {Let target = Event.target if (target.nodeName.toLowerCase ()!== ' button ') {Retu
          RN} Let Itemtitle =-$els. Itemtitle Let itemselections = this. $els. itemselections if (target.dataset.operation = = "Confirm") {if (This.questItem.type = = "textarea") {this.
          Addtextarea (Itemtitle)} else {this.addselections (Itemtitle, Itemselections)}
         } else {   This.handlecancel ()},} </script>
 

The above code is incomplete, but as you can see, v-el the control in an entity named "Item-title", we can extract it from this. $els. Itemtitle.

To use the property value of the control (the input value), you can do this:

this. $els. Itemtitle.value

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.