<template>
<div class="school">
<h2>学习名称:{{name}}</h2>
<h2>学习地址:{{address}}</h2>
</div>
</template>
<script>
export default {
name: "School",
data() {
return {
name: "尚硅谷",
address: "北京昌平"
}
},
}
</script>
<style>
.school {
background-color: gray;
}
</style>
<template>
<div>
<h1 v-text="msg" ref="msg"></h1>
<button @click="showDOM" ref="btn">点我显示上方DOM元素</button>
<School ref="sch"/>
</div>
</template>
<script>
// 引入School组件
import School from './components/School'
export default {
name: "App",
// 注册组件
components: {School},
data() {
return {
msg: "欢迎学习Vue"
}
},
methods: {
showDOM() {
console.log(this.$refs.msg); // 获取真实的DOM元素
console.log(this.$refs.btn);
console.log(this.$refs.sch); // 获取组件的实例对象
}
},
}
</script>
接收外部传入的数据
props: {
key: type
}
(3) 第三种方式,限制类型、必要性和必填只
props: {
key: {
type: String, // 类型
required: true, // 必传
default: "default" // 设置默认值
}
}
<template>
<div >
<h1 v-text="msg"></h1>
<h2>姓名:{{name}}</h2>
<h2>性别:{{sex}}</h2>
<h2>年龄:{{age}}</h2>
</div>
</template>
<script>
export default {
name: "Student",
data() {
return {
msg: "一个正在学Vue的人",
}
},
// 简单声明接收
// props: ["name", "age", "sex"]
// 接收的同时对类型进行限制
/* props: {
name: String,
age: Number,
sex: String
} */
// 接收同时对数据进行类型和必要性的限制
props: {
name: {
type: String, // 类型是字符串
required: true // 名字是必要的
},
age: {
type: Number,
default: 99
},
sex: {
type: String,
default: "男"
}
}
}
</script>
<template>
<div>
<student name="王老五" :age="18" ref="sch"/>
<hr>
<student name="碧莲" sex="女" ref="sch"/>
</div>
</template>
<script>
// 引入School组件
import Student from './components/Student.vue'
export default {
name: "App",
// 注册组件
components: {Student},
}
</script>
export const mixin = {
methods: {
showName() {
alert(this.name);
}
}
}
/*
export const 混合名 = {
option // option能写和组件一样的配置项
}
*/
export const mixin = {
methods: {
showName() {
alert(this.name);
}
}
}
export const mixin2 = {
data() {
return {
x: 1,
y: 2
}
},
}
<template>
<div >
<h2 @click="showName">学校名称:{{name}}</h2>
<h2>地址:{{address}}</h2>
</div>
</template>
<script>
// 引入mixin
// import {mixin, mixin2} from '../mixin'
export default {
name: "School",
data() {
return {
name: "尚硅谷",
address: "北京·昌平"
}
},
// mixins: [mixin, mixin2]
/* methods: {
showName() {
alert(this.name);
}
}, */
}
</script>
<template>
<div >
<h2 @click="showName">姓名:{{name}}</h2>
<h2>性别:{{sex}}</h2>
<h2>年龄:{{age}}</h2>
</div>
</template>
<script>
// 引入mixin,局部混合
// import {mixin} from '../mixin'
export default {
name: "Student",
data() {
return {
name: "张三",
sex: "男",
age: 18
}
},
// 局部混入
// mixins: [mixin]
/* methods: {
showName() {
alert(this.name);
}
}, */
}
</script>
// 引入Vue
import Vue from "vue";
// 引入App组件
import App from "./App"
// 引入mixin
import {mixin, mixin2} from './mixin'
// 关闭生产提示
Vue.config.productionTip = false
// 全局混合
Vue.mixin(mixin)
Vue.mixin(mixin2)
// 创建vm
new Vue({
render: h => h(App),
}).$mount("#app");
对象.install = function(Vue, options) {
// 添加全局过滤器
Vue.filter(...)
// 添加全局指令
Vue.directive(...)
// 配置全局混入
Vue.mixin(...)
// 添加实例方法
Vue.prototype.$MyMethod = function () {...}
Vue.prototype.$MyProperty = xxx
}
定义
export default {
install(Vue) {
console.log("定义一个插件", Vue);
// 全局过滤器
// 自定义指令
// 全局混入
// 原型上添加方法
}
}
使用:
// 引入Vue
import Vue from "vue";
// 引入App组件
import App from "./App"
// 引入插件
import plugins from './plugins'
// 关闭生产提示
Vue.config.productionTip = false
// 使用插件
Vue.use(plugins)
// 创建vm
new Vue({
render: h => h(App),
}).$mount("#app");