Vue全局配置

在开发环境下,Vue提供了全局配置对象,通过配置可以实现生产信息提示、警告忽略等人性化处理。

1.productionTip

当在网页中加载vue.js(开发版本)文件时,浏览器的控制器会出现英文的提示信息,提醒用户”您正在开发模式下运行Vue,在为生产部署时,请确保打开生产模式”。

如果希望在引入vue.js文件的情况下关闭提示信息,可以使用productionTip

通过Vue的全局配置productionTip可以控制生产信息的显示或隐藏。

Vue
<script>
        Vue.config.productionTip = false
</script>
2.silent

Vue全局配置对象中,silent可以取消Vue日志和警告,值类型为boolean,默认值为false,设为true表示忽略警告和日志,否则不忽略。

Vue
<script>
   Vue.config.silent = true
  </script>
3.devtools

在Vue全局配置中可以对该工具进行配置,将Vue.config.devtools设为true表示允许调试,否则不允许调试。

Vue
 <script>
   Vue.config.devtools = false
  </script>
4.组件进阶

在Vue中,组件是对结构的抽象,组件可复用性很强,每个组件拥有自己的作用域,区域之间独立工作互不影响,从而降低了代码的耦合度。

Vue还可以对组件的选项轻松完成合并,让组件的功能变得灵活,使用起来更加方便。

  • mixins  *******

mixins是一种分发Vue组件中可复用功能的方式。mixins对象可以包含任何组件选项,当组件使用mixins时,将定义的mixins对象引入组件中即可使用,mixins中的所有选项将会混入到组件自己的选项中。

JavaScript
var myMixin = {
created(){
this.hello()
},
methods:{
hello(){
console.log('hello from mixin!')
}
}
}
var Component = Vue.extend({
mixins:[myMixin]
})
var component = new Component()
  • render

在Vue中可以使用Vue.render()实现对虚拟DOM的操作。

在Vue中一般使用template来创建HTML,但这种方式可编程性不强,而使用Vue.render()可以更好地发挥JavaScript的编程能力。

Vue
<div id="app">
<my-component>成功渲染</my-component>
</div>
<script>
Vue.component('my-component',{
render(createElement){
return createElement('p',{
style:{
color:'red',
fontsize:'16px',
backgroundColor:'#eee'
}
}
,this.$slots.default)
}
})
var vm = new Vue({el:'#app'})
</script>
  • createElement

在使用渲染函数render()中可知,在render()函数的返回值中需要调用createElement()函数来创建元素。

需要注意的是,createElement()函数返回的并不是一个实际的DOM元素,它返回的其实是一个描述节点(createNodeDescription),用来告诉Vue在页面上需要渲染什么样的节点。

这个描述节点也可以称为虚拟节点(Virtual Node),简写为VNode。

而”虚拟DOM”是对由Vue组件树建立起来的整个VNode树的称呼。

createElement()函数的使用非常灵活,它的第一个参数可以是一个HTML标签名或组件选项对象;

第二个参数是可选,可以传入一个与模板中属性对应的数据对象;

第三个参数是由createElement()构建而成的子级虚拟节点,也可以使用字符串来生成文本虚拟节点。

Vue
<div id="app">
<my-component>
<template v-slot:header>
<div style="background-color: purple;height: 50px;">头部</div>
</template>
<template v-slot:content>
<div style="background-color:coral;height: 50px;">这是图书展示信息</div>
</template>
<template v-slot:footer>
<div style="background-color: deepskyblue;height: 50px;">这是底部信息</div>
</template>
</my-component>
</div>
<script>
Vue.component('my-component',{
render(createElement){
return createElement('div',[
createElement('header',this.$slots.header),
createElement('content',this.$slots.content),
createElement('footer',this.$slots.footer)
])
}
})
var vm = new Vue({el:'#app'})
</script>
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
滚动至顶部