VUE学习(十一)、组件——非单文件组件、单文件组件
一、非单文件组件
一个文件包含有n个组件
1、基本使用
<body>
<div id="root">
<hello></hello>
<hr />
<h1>{{msg}}</h1>
<hr />
<school></school>
<hr />
<student></student>
</div>
<div id="root2">
<hello></hello>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false;
const school = Vue.extend({
template: `
<div class="demo">
<h2>学校名称:{{schoolName}}</h2>
<h2>学校地址:{{address}}</h2>
<button @click="showName">点我提示学校名</button>
</div>
`,
data() {
return {
schoolName: "xxx大学",
address: "山东"
};
},
methods: {
showName() {
alert(this.schoolName);
}
}
});
const student = Vue.extend({
template: `
<div>
<h2>学生姓名:{{studentName}}</h2>
<h2>学生年龄:{{age}}</h2>
</div>
`,
data() {
return {
studentName: "张三",
age: 18
};
}
});
const hello = Vue.extend({
template: `
<div>
<h2>你好啊!{{name}}</h2>
</div>
`,
data() {
return {
name: "Tom"
};
}
});
Vue.component("hello", hello);
new Vue({
el: "#root",
data: {
msg: "你好啊!"
},
components: {
'school': school,
student
}
});
new Vue({
el: "#root2"
});
</script>
2、注意点
<body>
<div id="root">
<h1>{{msg}}</h1>
<school></school>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false;
const s = Vue.extend({
name: "myname",
template: `
<div>
<h2>学校名称:{{name}}</h2>
<h2>学校地址:{{address}}</h2>
</div>
`,
data() {
return {
name: "xx大学",
address: "云南"
};
}
});
new Vue({
el: "#root",
data: {
msg: "欢迎学习Vue!"
},
components: {
school: s
}
});
</script>
3、组件的嵌套
<body>
<div id="root">
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
const student = Vue.extend({
name:'student',
template:`
<div>
<h2>学生姓名:{{name}}</h2>
<h2>学生年龄:{{age}}</h2>
</div>
`,
data(){
return {
name:'xxx大学',
age:18
}
}
})
const school = Vue.extend({
name:'school',
template:`
<div>
<h2>学校名称:{{name}}</h2>
<h2>学校地址:{{address}}</h2>
<student></student>
</div>
`,
data(){
return {
name:'xxx大学',
address:'北京'
}
},
components:{
student
}
})
const hello = Vue.extend({
template:`<h1>{{msg}}</h1>`,
data(){
return {
msg:'欢迎来到xxx大学学习!'
}
}
})
const app = Vue.extend({
template:`
<div>
<hello></hello>
<school></school>
</div>
`,
components:{
school,
hello
}
})
new Vue({
template:'<app></app>',
el:'#root',
components:{app}
})
</script>
4、VueComponent
<body>
<div id="root">
<school></school>
<hello></hello>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
const school = Vue.extend({
name:'school',
template:`
<div>
<h2>学校名称:{{name}}</h2>
<h2>学校地址:{{address}}</h2>
<button @click="showName">点我提示学校名</button>
</div>
`,
data(){
return {
name:'xxx大学',
address:'北京'
}
},
methods: {
showName(){
console.log('showName',this)
}
},
})
const test = Vue.extend({
template:`<span>atguigu</span>`
})
const hello = Vue.extend({
template:`
<div>
<h2>{{msg}}</h2>
<test></test>
</div>
`,
data(){
return {
msg:'你好啊!'
}
},
components:{test}
})
const vm = new Vue({
el:'#root',
components:{school,hello}
})
</script>
5、 一个重要的内置关系
<body>
<div id="root">
<school></school>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
Vue.prototype.x = 99
const school = Vue.extend({
name:'school',
template:`
<div>
<h2>学校名称:{{name}}</h2>
<h2>学校地址:{{address}}</h2>
<button @click="showX">点我输出x</button>
</div>
`,
data(){
return {
name:'xxx大学',
address:'北京'
}
},
methods: {
showX(){
console.log(this.x)
}
},
})
const vm = new Vue({
el:'#root',
data:{
msg:'你好'
},
components:{school}
})
</script>
二、单文件组件
一个文件中只包含1个组件
1、School.vue
<template>
<!-- template组件的结构 -->
<div class="demo">
<h2>学校名称:{{ name }}</h2>
<h2>学校地址:{{ address }}</h2>
<button @click="showName">点我提示学校名</button>
</div>
</template>
<script>
//组件交互相关的代码
// 暴露相关知识 ES6模块化
// 第一种暴露方式(分别暴露)引入:import {xxx} from xxx
export const school = Vue.extend({
data() {
return {
name: "xxx大学",
address: "北京昌平"
};
},
methods: {
showName() {
alert(this.name);
}
}
});
/*
// 第二种暴露方式(统一暴露)引入:import {xxx} from xxx
export { school };
// 第三种暴露方式(默认暴露)(一般用这个)引入:import xxx from xxx
export default school
// 默认暴露简写
export default Vue.extend({
name: "School",
data() {
return {
name: "xxx大学",
address: "北京昌平"
};
},
methods: {
showName() {
alert(this.name);
}
}
})
// 或者
export default{
name: "School",
data() {
return {
name: "xxx大学",
address: "北京昌平"
};
},
methods: {
showName() {
alert(this.name);
}
}
}
*/
</script>
<style>
/* 组件的样式 */
.demo {
background-color: orange;
}
</style>
2、Student.vue
<template>
<div>
<h2>学生姓名:{{name}}</h2>
<h2>学生年龄:{{age}}</h2>
</div>
</template>
<script>
export default {
name:'Student',
data(){
return {
name:'张三',
age:18
}
}
}
</script>
3、main.js
import App from "./App.vue";
new Vue({
el: "#root",
template: `<App></App>`,
components: { App }
});
4、index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>练习一下单文件组件的语法</title>
</head>
<body>
<div id="root">
</div>
</body>
<script src="../js/vue.js"></script>
<script src="./main.js"></script>
</html>
|