【Vue 从入门到实战 进阶式掌握完整知识体系】028
【Vue 从入门到实战 进阶式掌握完整知识体系】028
5、插件的定义和使用基本写法代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equi
【Vue 从入门到实战 进阶式掌握完整知识体系】028
5、插件的定义和使用
基本写法
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
// plugnin 插件:也是把通用性的功能封装起来
// 定义插件
ct myPlugin = {
install(app, opti){
// 这个app就是根组件,通过app可以做很多事情
cole.log(app, opti);
}
}
ct app = ({
template: `
<my-title />
`
});
appponent("my-title", {
template: `
<div>hello world!</div>
`
});
// 使用插件,此时可以传入参数
app.use(myPlugin, { name: "zibo" });
ct vm = ('#root');
</script>
</html>
运行结果
使用插件做更多事情
代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello vue</title>
<!-- 引入Vue库 -->
<script src="@next"></script>
</head>
<body>
<div id="root"></div>
</body>
<script>
// plugnin 插件:也是把通用性的功能封装起来
// 定义插件
ct myPlugin = {
install(app, opti){
// 这个app就是根组件,通过app可以做很多事情
// 为后辈(子孙)组件添加属性
app.provide('name', 'zibo');
// 自定义指令
app.directive('focus', {
mounted(el){
el.focus();
}
});
// 扩展全局属性
globalProperties.$sayHello = "hello zibo!";
}
}
ct app = ({
template: `
<my-title />
`
});
appponent("my-title", {
// 接收最顶层组件(祖上)传过来的属性
inject: ['name'],
mounted(){
cole.log(this.$sayHello);
},
// 接收之后,在模板里面可以直接使用
template: `
<div>
<div>hello world!</div>
<div>{{name}}</div>
<input v-focus />
</div>
`
});
// 使用插件,此时可以传入参数
app.use(myPlugin, { name: "zibo" });
ct vm = ('#root');
</script>
</html>
运行结果
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
上传时间: 2025-07-23 17:41:52
推荐阅读
留言与评论(共有 18 条评论) |
本站网友 高谭市的沉沦 | 25分钟前 发表 |
` <my-title /> ` }); appponent("my-title" | |
本站网友 徐启武 | 3分钟前 发表 |
{ name | |
本站网友 湛江钢铁 | 3分钟前 发表 |
{ // 接收最顶层组件(祖上)传过来的属性 inject | |
本站网友 卓建荣 | 30分钟前 发表 |
initial-scale=1.0"> <title>hello vue</title> <!-- 引入Vue库 --> <script src="@next"></script> </head> <body> <div id="root"></div> </body> <script> // plugnin 插件:也是把通用性的功能封装起来 // 定义插件 ct myPlugin = { install(app | |
本站网友 阜宁二手房网 | 16分钟前 发表 |
在模板里面可以直接使用 template | |
本站网友 钓鱼台国宾馆价格 | 15分钟前 发表 |
{ name | |
本站网友 luobu | 18分钟前 发表 |
['name'] | |
本站网友 photoshop最新版 | 22分钟前 发表 |
` <div> <div>hello world!</div> <div>{{name}}</div> <input v-focus /> </div> ` }); // 使用插件 | |
本站网友 特色农业 | 15分钟前 发表 |
"zibo" }); ct vm = ('#root'); </script> </html>运行结果image-20210614150600442.png使用插件做更多事情代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width | |
本站网友 annotate | 19分钟前 发表 |
此时可以传入参数 app.use(myPlugin | |
本站网友 dlan | 5分钟前 发表 |
mounted(){ cole.log(this.$sayHello); } | |
本站网友 打道回府 | 20分钟前 发表 |
// 接收之后 | |
本站网友 大城市铁岭 | 21分钟前 发表 |
` <my-title /> ` }); appponent("my-title" | |
本站网友 日用消费品 | 29分钟前 发表 |
在模板里面可以直接使用 template | |
本站网友 cda文件 | 0秒前 发表 |
// 接收之后 | |
本站网友 北京整形医院哪家好 | 2分钟前 发表 |
插件的定义和使用基本写法代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width | |
本站网友 炒王不留行 | 27分钟前 发表 |
initial-scale=1.0"> <title>hello vue</title> <!-- 引入Vue库 --> <script src="@next"></script> </head> <body> <div id="root"></div> </body> <script> // plugnin 插件:也是把通用性的功能封装起来 // 定义插件 ct myPlugin = { install(app |