


非单文件组件: 一个文件中包含有n个组件
Vue中使用组件的三大步骤:
如何定义一个组件:
如何注册组件
Vue.cpmponent('组件名',组件)编写组件标签 :
基本使用
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>Documenttitle>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
<div id="root">
<school>school>
<hr>
<student>student>
div>
<div id="root2">
<student2>student2>
div>
<script>
Vue.config.produtionTip = false
// 创建school组件
const school = Vue.extend({
template: `
学校姓名:
`,
data() {
return {}
}
})
// 创建student
const student = Vue.extend({
template: `
学生姓名
`
})
const student2 = Vue.extend({
template: `
safafad
`
})
Vue.component('student2', student2)
new Vue({
el: '#root',
components: {
school,
student
}
})
new Vue({
el: '#root2'
})
script>
body>
html>
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>Documenttitle>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
<div id="root">
<app>app>
div>
body>
<script>
Vue.config.productionTip = false;
const student = Vue.extend({
name: 'student',
template: `
学生姓名: {{name}}
学生年龄: {{age}}
`,
data() {
return {
name: '牛婷婷',
age: 15
}
}
})
// 创建school组件
const school = Vue.extend({
name: 'school',
template: `
学校姓名: {{schoolName}}
地址: {{address}}
`,
data() {
return {
schoolName: '台历',
address: '金钟'
}
},
components: {
student
}
})
// 创建hello组件
const helloo = Vue.extend({
name: 'hello',
template: `
Hello
`
})
const app = Vue.extend({
name: 'app',
template: `
`,
components: {
school,
helloo
}
})
// 创建vm
new Vue({
el: '#root',
// 第二步:注册组件(局部注册)
components: {
app
}
})
script>
html>
<template>
<div>
<School>School>
<Student>Student>
div>
template>
<script>
import School from './School'
import Student from './Student'
export default {
name: 'App',
components: {
School,
Student
}
}
script>
<style>
style>
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>Documenttitle>
head>
<body>
<div id="root">
<App>App>
div>
<div id="root">div>
<script src="./main.js">script>
body>
html>
import App from './App.veu'
new Vue({
el: '#root',
components: { App }
})
<template>
<div class="demo">
<h2>学校姓名: {{schoolName}}h2>
<h2>地址: {{address}}h2>
div>
template>
<script>
// export default {
// }
export default ({
name:'school',
data() {
return {
schoolName: '台历',
address: '金钟'
}
}
})
script>
<style>
style>
<template>
<div class="demo">
<h2>学校姓名: {{name}}h2>
<h2>地址: {{age}}h2>
div>
template>
<script>
export default({
name:'school',
data() {
return {
name: '牛婷婷',
age: 18
}
}
})
script>
<style>
style>
关于组件标签:
···
关于 VueComponent:

会帮我们创建school组件的实例对象,即Vue帮我们执行的: new VueComponent(options)关于this指向:
__proto__ == Vue.prototype