Home  >  Article  >  Web Front-end  >  Detailed explanation of VUE component mounting method

Detailed explanation of VUE component mounting method

php中世界最好的语言
php中世界最好的语言Original
2018-06-15 15:16:446914browse

This time I will give you a detailed explanation of the mounting method of VUE components, and what are the precautions for mounting VUE components. The following is a practical case, let's take a look.

1. Three mounting methods for components

Automatic mounting

var app3 = new Vue({
 el: '#app-3',
 data: {
 seen: true
 }
})

Manual mounting

// 可以实现延迟按需挂载
<p id="app"> {{name}} </p> 
<button onclick="test()">挂载</button> 
<script> 
 var obj= {name: '张三'} 
 var vm = new Vue({ 
 data: obj
 }) 
 function test() { 
 vm.$mount("#app"); 
 }
// Vue.extend()创建没有挂载的的子类,可以使用该子累创建多个实例
var app= Vue.extend({ 
 template: '<p>{{message}}</p>', 
 data: function () { 
 return { 
  message: 'message'
  } 
 } 
 }) 
 new app().$mount('#app') // 创建 app实例,并挂载到一个元素上

2 , The way of routing parameters

<p>
  <!-- query要用path来引入,params要用name来引入,故不能写为 :to="{path:&#39;/login&#39;,params: {isLogin: true}} -->
  <!-- 跳转路由时用this.$router: this.$router.push({name:"login",params:{isLogin:true}});this.$router.push({path: &#39;/login&#39;, query: {isLogin : true}}); -->
  <!-- 接收参数时用this.$route: this.$route.query.isLogin 和 this.$route.params.isLogin; -->
  <router-link :to="{name:&#39;login&#39;,params: {isLogin: true}}">亲,请登录</router-link>
  <router-link :to="{name:&#39;login&#39;,params: {isLogin: false}}">免费注册</router-link>
 </p>
 <!-- 路由出口, 路由匹配到的组件将渲染在这里 -->
 <router-view></router-view>

3. Understanding render:h => h(App)

render:h=>h (App) is the arrow function writing method in ES6, which is equivalent to render:function(h){return h(App);}.

1. This in the arrow function points outside the function that wraps this. on the object.

2.h is the alias of creatElement, the general management of the vue ecosystem

3.template:'', components:{App} used in conjunction with render alone :h=>h(App) will achieve the same effect

The former recognizes the