希望:
web components 通过创建封装好功能的定制元素解决上述问题
Vue.component(组件名称,{
data: 组件数据;
template: 组件模板内容
})
注意:
<head>
<title>注册组件title>
head>
<body>
<div id="app">
<button-counter>button-counter>
div>
<script src="../vue.js">script>
<script>
// 注册组件
Vue.component('button-counter', {
data: function() {
return {
count: 0
}
},
template: ""
})
var vm = new Vue({
el: "#app",
data: {
}
})
script>
body>
只能在当前注册它的vue示例中使用;
<head>
<title>注册组件title>
head>
<body>
<div id="app">
<button-counter>button-counter>
<hello-world>hello-world>
<hello-tom>hello-tom>
div>
<script src="../vue.js">script>
<script>
// 注册组件
Vue.component('button-counter', {
data: function() {
return {
count: 0
}
},
template: ""
});
var HelloWorld = {
data: function(){
return {
msg: 'HelloWorld'
}
},
template: '{{msg}}'
};
var HelloTom = {
data: function() {
return {
msg: 'HelloTom'
}
},
template: '{{msg}}'
};
var vm = new Vue({
el: "#app",
data: {
},
components: {
// 注册局部组件
'hello-world': HelloWorld,
'hello-tom': HelloTom
}
});
script>
body>
网址: https://github.com/vuejs/vue-devtools
npm install
Vue.component('menu-item', {
props: ['title'],
template: `
{{title}}`
})
<div id="app">
<div>{{pmsg}}div>
<menu-item title='来自父组件的值'>menu-item>
<menu-item :title='ptitle' content='hello'>menu-item>
div>
<script type="text/javascript">
Vue.component('menu-item', {
// 3、 子组件用属性props接收父组件传递过来的数据
props: ['title', 'content'],
data: function() {
return {
msg: '子组件本身的数据'
}
},
template: '{{msg + "----" + title + "-----" + content}}'
});
var vm = new Vue({
el: '#app',
data: {
pmsg: '父组件中内容',
ptitle: '动态绑定属性'
}
});
script>
$emit() 第一个参数为 自定义的事件名称 第二个参数为需要传递的数据;
<div id="app">
<div :style='{fontSize: fontSize + "px"}'>{{pmsg}}div>
<menu-item :parr='parr' @enlarge-text='handle($event)'>menu-item>
div>
<script type="text/javascript" src="js/vue.js">script>
<script type="text/javascript">
/*
子组件向父组件传值-携带参数
*/
Vue.component('menu-item', {
props: ['parr'],
template: `
- {{item}}
### 1、子组件用$emit()触发事件
### 第一个参数为 自定义的事件名称 第二个参数为需要传递的数据
`
});
var vm = new Vue({
el: '#app',
data: {
pmsg: '父组件中内容',
parr: ['apple','orange','banana'],
fontSize: 10
},
methods: {
handle: function(val){
// 扩大字体大小
this.fontSize += val;
}
}
});
script>
var eventHub = new Vue()
eventHub.$on('add-todo', addTodo)
eventHub.$off('add-todo')
eventHub.$emit('add-todo', id)
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>兄弟组件传值title>
head>
<body>
<div id="app">
<test-tom>test-tom>
<test-jerry>test-jerry>
div>
<script src="../vue.js">script>
<script>
/* 兄弟组件间传值 */
// 事件中心
var hub = new Vue();
Vue.component('test-tom', {
data: function() {
return {
num: 0
}
},
template: `
Tom: {{num}}
`,
methods: {
handle: function() {
// 触发兄弟组件的事件
hub.$emit('jerry-event', 1);
}
},
mounted: function() {
// 监听事件
hub.$on('tom-event', (val) => {
this.num += val;
})
}
});
Vue.component('test-jerry', {
data: function() {
return {
num: 0
}
},
template: `
Jerry: {{num}}
`,
methods: {
handle: function() {
// 触发兄弟组件的事件
hub.$emit('tom-event', 2);
}
},
mounted: function() {
// 监听事件
hub.$on('jerry-event', (val) => {
this.num += val;
})
}
})
var vm = new Vue({
el: "#app",
data: {
}
})
script>
body>
Vue.component('alert-box', {
template: `
Error!
`
})
<alert-box>someting bad requestd!alert-box>
<head>
<title>具名插槽title>
head>
<body>
<div id="app">
<base-layout>
<p slot='header'>标题信息p>
<p>主要内容1p>
<p>主要内容2p>
<p slot='footer'>底部信息信息p>
base-layout>
<base-layout>
<template slot='header'>
<p>标题信息1p>
<p>标题信息2p>
template>
<p>主要内容1p>
<p>主要内容2p>
<template slot='footer'>
<p>底部信息信息1p>
<p>底部信息信息2p>
template>
base-layout>
div>
<script type="text/javascript" src="../vue.js">script>
<script type="text/javascript">
/*
具名插槽
*/
Vue.component('base-layout', {
template: `
### 1、 使用 中的 "name" 属性绑定元素 指定当前插槽的名字
`
});
var vm = new Vue({
el: '#app',
data: {
}
});
script>
body>
<head>
<title>作用域插槽title>
<style>
.current {
color: orange;
}
style>
head>
<body>
<div id="app">
<fruit-list :list='list'>
<template slot-scope='slotProps'>
<strong :class="slotProps.info.id == 2?'current': ''">{{slotProps.info.name}}strong>
template>
fruit-list>
div>
<script src="../vue.js">script>
<script>
/* 作用域插槽 */
Vue.component('fruit-list', {
props: ['list'],
template: `
{{item.name}}
`
});
var vm = new Vue({
el: "#app",
data: {
list: [
{
id:1,
name: 'apple'
}, {
id: 2,
name: 'orange'
}, {
id: 3,
name: 'banana'
}
]
}
})
script>
body>
按照组件化方式实现业务需求:
功能实现步骤:
<head>
<title>购物车title>
<style>
.container .cart{
width: 300px;
margin: auto;
}
.container .title {
background-color: lightblue;
height: 40px;
line-height: 40px;
text-align: center;
}
.container .total {
background-color: #FFCE46;
height: 50px;
line-height: 50px;
text-align: right;
}
.container .total button {
margin: 0 10px;
background-color: #DC4C40;
height: 35px;
width: 80px;
border: 0;
color: #fff;
}
.container .total span {
color: red;
font-weight: bold;
}
.container .item {
height: 55px;
line-height: 55px;
position: relative;
border-top: 1px solid #ADD8E6;
}
.container .item img {
width: 45px;
height: 45px;
margin: 5px;
}
.container .item .name {
position: absolute;
width: 90px;
top: 0;
left: 55px;
font-size: 16px;
}
.container .item .change {
width: 100px;
position: absolute;
top: 0;
right: 50px;
}
.container .item .change a {
font-size: 20px;
width: 30px;
text-decoration:none;
background-color: lightgray;
vertical-align: middle;
}
.container .item .change .num {
width: 40px;
height: 25px;
}
.container .item .del {
position: absolute;
top: 0;
right: 0px;
width: 40px;
text-align: center;
font-size: 40px;
cursor: pointer;
color: red;
}
.container .item .del:hover {
background-color: orange;
}
style>
head>
<body>
<div id="app">
<div class="container">
<my-cart>my-cart>
div>
div>
<script src="../../vue.js">script>
<script>
var cartTitle = {
props: ['uname'],
template: `
{{uname}}的商品
`
};
var cartList = {
props: ['list'],
template: `
`,
methods: {
sub:function(id) {
this.$emit('change-num', {
id:id,
type: "sub"
})
},
add: function(id) {
this.$emit('change-num', {
id:id,
type: "add"
})
},
changeNum: function(id, event) {
this.$emit('change-num', {
id: id,
num: event.target.value,
type: "change"
});
},
del: function(id) {
// 把id传递给父组件
this.$emit('cart-del', id);
}
}
};
var cartTotal = {
props: ['list'],
template: `
总价:{{total}}
`,
computed: {
total: function() {
// 计算商品总价
var t = 0;
this.list.forEach(item => {
t += (item.price * item.num);
})
return t;
}
}
};
Vue.component('my-cart', {
data: function() {
return {
uname: '张三',
list: [
{
id: 1,
name: "TCL电视",
price: 1000,
num: 1,
img: 'images/a.jpg'
},
{
id: 2,
name: "机顶盒",
price: 1000,
num: 2,
img: 'images/b.jpg'
},
{
id: 3,
name: "海尔冰箱",
price: 1000,
num: 3,
img: 'images/c.jpg'
},
{
id: 4,
name: "小米手机",
price: 1000,
num: 4,
img: 'images/d.jpg'
},
{
id: 5,
name: "pptv",
price: 1000,
num: 5,
img: 'images/e.jpg'
}
]
}
},
template: `
`,
components: {
'cart-title': cartTitle,
'cart-list': cartList,
'cart-total': cartTotal
},
methods: {
changeNum: function(val) {
this.list.some(item => {
if (val.type == "add") {
if(item.id == val.id) {
item.num += 1;
}
} else if(val.type == "sub"){
if(item.id == val.id) {
item.num -=1;
}
} else {
if(item.id == val.id) {
item.num = val.num;
}
}
})
},
delCart:function(id){
// 根据id删除list中对应的数据
// 1. 找到id对应的数据
var index = this.list.findIndex(item => {
return item.id == id;
});
// 2. 根据索引删除数据
this.list.splice(index, 1);
},
}
})
var vm = new Vue({
el: "#app",
data: {
}
});
script>
body>