一、组件化编程——非单文件组件
- 模板编写没有提示
- 没有构建过程, 无法将 ES6 转换成 ES5
- 不支持组件的 CSS
- 真正开发中几乎不用
(一)基本使用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>基本使用</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<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:'BUBA',
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,
student
}
})
new Vue({
el:'#root2',
})
</script>
</html>
(二)几个注意点
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>几个注意点</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<h1>{
{msg}}</h1>
<school></school>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false
const s = Vue.extend({
name:'atguigu',
template:`
<div>
<h2>学校名称:{
{name}}</h2>
<h2>学校地址:{
{address}}</h2>
</div>
`,
data(){
return {
name:'BUBA',
address:'北京'
}
}
})
new Vue({
el:'#root',
data:{
msg:'欢迎学习Vue!'
},
components:{
school:s
}
})
</script>
</html>
(三)组件的嵌套
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>组件的嵌套</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<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:'BUBA',
age:18
}
}
})
const school = Vue.extend({
name:'school',
template:`
<div>
<h2>学校名称:{
{name}}</h2>
<h2>学校地址:{
{address}}</h2>
<student></student>
</div>
`,
data(){
return {
name:'BUBA',
address:'北京'
}
},
components:{
student
}
})
const hello = Vue.extend({
template:`<h1>{
{msg}}</h1>`,
data(){
return {
msg:'欢迎来到BUBA学习!'
}
}
})
const app = Vue.extend({
template:`
<div>
<hello></hello>
<school></school>
</div>
`,
components:{
school,
hello
}
})
new Vue({
template:'<app></app>',
el:'#root',
components:{
app}
})
</script>
</html>
(四)VueComponent