DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>非单文件组件title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
<div id="root">
<hello>hello>
<xuexiao>xuexiao>
<hr>
<student>student>
代码的复用:<student>student>
div>
<hr>
<div id="root2">
<h2>第二个vue实例h2>
<hello>hello>
div>
body>
<script>
//1.创建一个全局组件
const hello=Vue.extend({
template:`
你好啊,{{name}}欢迎光临!
`,
data(){
return{
name:"张三"
}
}
})
//2.全局注册组件
Vue.component("hello",hello)
// 第一步:创建school组件
const school=Vue.extend({
// el:"#root", 组件定义时,一定不要写el配置项,因为最终所有的组件都要被一个vm管理,由vm决定服务于哪个容器。
template:`
学校名称:{{schoolName}}
学校地址:{{schoolAddress}}
`,
data(){
return{
schoolName:"幸福中学",
schoolAddress:"重庆渝北"
}
},
methods: {
showName(){
alert(this.schoolName)
}
},
})
// 第一步:创建student组件
const student=Vue.extend({
template:`
学生姓名:{{studentName}}
学生年龄:{{studentAge}}
`,
data(){
return{
studentName:"张三",
studentAge:20
}
}
})
//创建vm
new Vue({
el:"#root",
// 第二步:注册组件(局部注册)
components:{
xuexiao:school, //key和value不同名
student //key和value同名时
}
})
new Vue({
el:"#root2"
})
script>
html>
Vue中使用组件的三大步骤:
一、定义组件(创建组件)
二、注册组件
三、使用组件(写组件标签)
几种组件的认可命名方式
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>非单文件组件title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
<div id="app">
<h1>{{msg}}h1>
<hr>
<my-school>my-school>
<student>student>
<Teacher>Teacher>
<clear>clear>
<blackboard>blackboard>
div>
body>
<script>
Vue.config.productionTip=false;
const school=Vue.extend({
template:`
学校名称:{{name}}
学校地址:{{address}}
`,
data(){
return{
name:"幸福中学",
address:"重庆市渝北区"
}
}
})
const student=Vue.extend({
template:`
学生姓名:{{name}}
学生年龄:{{age}}
`,
data(){
return{
name:"张三",
age:17
}
}
})
const teacher=Vue.extend({
template:`
老师姓名:{{name}}
老师性别:{{sex}}
`,
data(){
return{
name:"李四",
sex:"女"
}
}
})
const clear=Vue.extend({
name:"Cleaner",
template:`
清理工姓名:{{name}}
`,
data(){
return{
name:"王五"
}
}
})
const blackboard={
name:"Cleaner",
template:`
黑板型号:{{bid}}
`,
data(){
return{
bid:26565
}
}
}
new Vue({
el:"#app",
data:{
msg:"早上好啊,又是元气满满的一天啊!"
},
components:{
//几种组件的认可命名方式
'my-school':school,
student,
Teacher:teacher,
clear,
blackboard
}
})
script>
html>
几个注意点:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件的嵌套title>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
<div id="root">
div>
body>
<script>
Vue.config.productionTip=false;
//定义student组件
const student=Vue.extend({
template:`
学生姓名:{{name}}
学生年龄:{{age}}
`,
data(){
return{
name:"张三",
age:20
}
}
})
//定义school组件
const school=Vue.extend({
template:`
学校名称:{{name}}
学校地址:{{address}}
`,
data(){
return{
name:"幸福中学",
address:"重庆市渝北区"
}
},
//注册student组件(局部)
components:{
student
}
})
//定义hello组件
const hello=Vue.extend({
template:`
欢迎学习{{knowledge}}
`,
data(){
return{
knowledge:"Vue"
}
}
})
//定义app组件
const app=Vue.extend({
template:`
`,
//注册组件(局部)
components:{
hello,
school
}
})
//创建vm
new Vue({
el:"#root",
template:`
`,
//注册组件(局部)
components: {app}
})
script>
html>
关于VueComponent:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
body>
<script>
Vue.config.productionTip=false;
//定义一个构造函数
function Demo(){
this.x=1
this.y=2
}
//创建一个Demo()实例对象
const d=new Demo()
console.log("Demo.prototype",Demo.prototype) //显示原型属性
console.log("d.__proto__",d.__proto__) //隐式原型属性
//程序员通过显示原型属性操作原型对象,追加一个x属性,值为99
Demo.prototype.n=99
console.log("d.__proto__.n",d.__proto__.n)
console.log("d.n",d.n)
console.log("d.__proto__===Demo.prototype",d.__proto__===Demo.prototype)
console.log("d",d)
script>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
head>
<body>
<div id="root">
<school>school>
div>
body>
<script>
Vue.config.productionTip=false;
Vue.prototype.x=99
const school=Vue.extend({
template:`
学校名称:{{name}}
学校地址:{{address}}
`,
data(){
return{
name:"幸福中学",
address:"重庆"
}
},
methods:{
showX(){
console.log("x:",this.x)
}
}
})
const vm=new Vue({
el:"#root",
components:{school}
})
console.log("school.prototype.__proto__===Vue.prototype:",school.prototype.__proto__===Vue.prototype)
script>
html>
结构:
Abc.vue
<template>
template>
<script>
// 组件交互相关的代码
script>
<style>
/*组件的样式*/
style>
<template>
<div id="root">
<h2>学生姓名:{{name}}h2>
<h2>学生年龄:{{age}}h2>
div>
template>
<script>
export default {
name:"Student",
data(){
return{
name:"张三",
age:19
}
}
}
script>
School.vue:
<template>
<div id="root">
<h2>学校名称:{{name}}h2>
<h2>学校地址:{{address}}h2>
<button @click="showName">点我提示学校名button>
div>
template>
<script>
export default {
name:"School",
data(){
return{
name:"幸福中学",
address:"重庆市"
}
},
methods:{
showName(){
alert(this.name)
}
}
}
script>
<style>
#root{
background-color: pink;
}
style>
App.vue:
<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>
main.js
import App from "./App.vue"
new Vue({
el:"#root",
template:`
`,
components:{
App
}
})
index.html
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>练习使用单文件组件语法title>
head>
<body>
<div id="root">div>
<script src="../js/vue.js">script>
<script src="./main.js">script>
body>
html>
因为还没有vue脚手架,所以程序还暂时执行不了