Vue的命令式组件是一种封装方法,它将功能逻辑封装在组件内部,并通过命令式的方式进行调用和控制。
以下是一个简单的示例代码,演示如何使用命令式调用组件:1. 编写message.vue文件:
<template>
<div class="message" v-if="show">
{{label}}
</div>
</template>
<script>
export default {
data() {
return {
label: '空消息',
show: true
}
}
}
</script>
<style lang="scss">
.message {
position: fixed;
top: 20px;
right: 10px;
color: #fff;
background-color: #222;
padding: 15px 20px;
width: 180px;
}
</style>2. 实现调用逻辑,messagefun.js文件:import Vue from 'vue'
import Message from './message.vue'
const Msg = Vue.extend(Message);
export default (options = {}) => {
const instance = new Msg({
el: document.createElement('div'),
data() {
return {
label: options.label
}
}
})
document.body.appendChild(instance.$el);
window.setTimeout(() => {
instance.$data.show = false
}, options.time ? options.time : 3000)
}3. 挂载到原型,index.js里:import Vue from 'vue'
import P3 from 'pages/index3.vue'
import Coms from 'pages/page3com/com.js'
import msg from 'pages/page3com/messagefun.js'
Vue.use(Coms)
Vue.prototype.$msg = msg;
new Vue({
el: '#p3',
render: h => h(P3)
})
4. 调用:this.$msg({
label: '我是一个消息',
time: 4000
});
网友回复
如何用html写出网页滚动视频播放卡片视觉差异效果的代码?
程序员如何低成本搭建代理进行科学上网学习技术?
threejs如何做个三维搭积木的游戏?
three如何实现标记多个起始路过地点位置后选择旅行工具(飞机汽车高铁等),最后三维模拟行驶动画导出mp4?
ai实时驱动的3d数字人可视频聊天的开源技术有吗
swoole+phpfpm如何实现不同域名指向不同目录的多租户模式?
如何用go替换nginx实现请求phpfpm解析运行php脚本?
有没有浏览器离线运行进行各种文档、图片、视频格式转换的开源工具?
如何使用go语言搭建一个web防火墙?
linux如何检测特定网络协议比如http协议中报文是否包含特点关键词并阻止返回给客户?


