构建子组件Test
<!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>vueExtend</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="root">
<Test :msg="message" />
</div>
<script>
//Vue.extend返回一个构造函数
const component = Vue.extend({
template:'<div>{
{msg}}</div>',
props:{
msg:{
type:String
}
}
})
Vue.component('Test',component) //根据源码,这里是挂载到全局上来的
new Vue({
el:"#root",
data(){
return{
message:'test component'
}
}
})
</script>
</body>
</html>
vue extend的进阶用法
- 需求希望可以自定义像
this.$on this.$emit this.$set
这种方式实现一个loading
this.$loading( '正在加载....')
分析:
- 1.要想实现
this.$loading()
那么必须是在vue的实例上增加一个属性也就是Vue.prototype.$loading
并且这个属性是一个方法 - 2.定义这个方法
实现
<!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>vueLoading</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="root">
<button @click="showLoading">显示loading</button>
</div>
<script>
// vue.extend 可以返回一个构造函数
const LoadingComponent = Vue.extend({
template:'<div id="loading-wrapper">{
{msg}}</div>',
props:{
msg:{