您的位置:首页 > Web前端 > Vue.js

vue组件小扩展

2019-04-24 21:42 183 查看
  1. 动态组件
      动态组件就是 component组件 , 组件身上可以绑定一个is属性, 用来表示某一个组件
      例:
<div id="app">
<keep-alive>
<component is = "B"></component>
</keep-alive>
</div>
</body>
<script>
new Vue({
el: '#app',
components: {
"A": {
template: '<div> A </div>'
},
"B": {
template: '<div> B </div>'
}
}
})
</script>
  • 动态属性切换
<body>
<div id="app">
<button @click='type=type==="A" ? "B": "A"'>切换</button>
<component :is="type"></component>
</div>
</body>
<script>
new Vue({
el: '#app',
data: {
type: 'B'
},
components: {
"A": {
template: '<div>A</div>'
},
"B": {
template: '<div>B</div>'
}
}

})
</script>
  1. is属性
      我们html中有一些标签是规定它的直接子元素的 , 比如 ul li ol li selector option table这类型标签
    • 不能直接用常规的组件使用方式, 必须在对应直接子元素上绑定 is属性
<div id="app">
<table>
<tr>
<td>aa</td>
<td>bb</td>
<td>cc</td>
</tr>
<!-- is = 组件名称 -->
<tr is = 'my-table'></tr>
</table>
</div>
  1. keep-alive组件
    将组件的内容存在浏览器缓存中, 当我们需要重复渲染的时候, 就从浏览器缓存中去调用,这样可以减少性能消耗
<keep-alive>
<component is = "A"></component>
</keep-alive>
  1. 不常用指令
      v-text vs v-html 都是用来将数据插入DOM中, 但是v-html可以解析标签类型数据
  2. v-cloak 解决 {{}} 语法短暂原样输出
  3. v-pre 原样输出
  4. v-once 只渲染一次
  5. 异步组件
      异步组件也是一个函数, 只不过这个函数使用Promise,函数有一个返回值
      例:
<body>
<div id="app">
<jia_async></jia_async>
</div>

</body>
<script>
var result = new Promise(function(resolve, reject) {
resolve({
template: '<div> 异步组件 </div>'
})
})
Vue.component('jia_async', async() => {
const tpl = await result.then(res => res)
return tpl
})

new Vue({
el: '#app',
})
</script>
  1. 高阶组件(HOC) higt order component [ol] 高阶组件是一个函数, 这个函数的作用是进行组件的复用
    例:
[/ol]
class HOC {
//复用性代码
sum(){
//一万行代码
return 1+1
}
init(Com){
return <Com sum = { this.sum }/>
}
}

export default {
init: HOC.init
}

const A = init(A)
con
20000
st B = init(B)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: