【IEEE出版 | 会后3-4个月EI检索】第三届云计算、大数据应用与软件工程国际学术会议 (CBASE 2024)_艾思科蓝_学术一站式服务平台
更多学术会议请看:学术会议-学术交流征稿-学术会议在线-艾思科蓝
目录
引言
一、Vue.js 组件开发基础
1.1 组件的概念
1.2 创建组件
全局注册
Vue.component('my-component', {
template: '<div>A custom component!</div>'
});
局部注册
Vue.component('parent-component', {
template: `
<div>
<my-component></my-component>
</div>
`,
components: {
'my-component': {
template: '<div>A custom component!</div>'
}
}
});
1.3 单文件组件(SFC)
<template>
<div class="my-component">
A custom component!
</div>
</template>
<script>
export default {
name: 'MyComponent'
};
</script>
<style scoped>
.my-component {
color: red;
}
</style>
1.4 父子组件通信
父组件向子组件传递数据(props)
<!-- ParentComponent.vue -->
<template>
<div>
<child-component :message="parentMessage"></child-component>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent
},
data() {
return {
parentMessage: 'Hello from Parent!'
};
}
};
</script>
<!-- ChildComponent.vue -->
<template>
<div>{{ message }}</div>
</template>
<script>
export default {
props: {
message: {
type: String,
required: true
}
}
};
</script>
子组件向父组件发送消息(事件)
<!-- ParentComponent.vue -->
<template>
<div>
<child-component @notify="handleNotify"></child-component>
</div>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent
},
methods: {
handleNotify(message) {
console.log(message);
}
}
};
</script>
<!-- ChildComponent.vue -->
<template>
<div>
<button @click="notifyParent">Notify Parent</button>
</div>
</template>
<script>
export default {
methods: {
notifyParent() {
this.$emit('notify', 'Hello from Child!');
}
}
};
</script>
二、构建高效可复用组件
2.1 组件设计原则
单一职责原则
开闭原则
接口隔离原则
2.2 使用 props 和事件进行通信
2.3 插槽(Slots)
默认插槽
<!-- ChildComponent.vue -->
<template>
<div class="child-component">
<slot></slot>
</div>
</template>
<!-- ParentComponent.vue -->
<template>
<div>
<child-component>
<p>This content will be rendered inside the child component.</p>
</child-component>
</div>
</template>
具名插槽
<!-- ChildComponent.vue -->
<template>
<div class="child-component">
<slot name="header"></slot>
<slot></slot> <!-- 默认插槽 -->
<slot name="footer"></slot>
</div>
</template>
<!-- ParentComponent.vue -->
<template>
<div>
<child-component>
<template v-slot:header>
<h1>This is the header</h1>
</template>
<p>This content will be rendered in the default slot.</p>
<template v-slot:footer>
<p>This is the footer</p>
</template>
</child-component>
</div>
</template>
作用域插槽
<!-- ChildComponent.vue -->
<template>
<div class="child-component">
<slot :user="user"></slot>
</div>
</template>
<script>
export default {
data() {
return {
user: {
name: 'John Doe',
age: 30
}
};
}
};
</script>
<!-- ParentComponent.vue -->
<template>
<div>
<child-component v-slot:default="slotProps">
<p>User Name: {{ slotProps.user.name }}</p>
<p>User Age: {{ slotProps.user.age }}</p>
</child-component>
</div>
</template>
三、Vue.js组件的高级特性
1. 单文件组件(.vue文件)
<template>
<div class="hello">
<h1>{{ msg }}</h1>
</div>
</template>
<script>
export default {
name: 'Hello',
props: {
msg: String
}
};
</script>
<style scoped>
h1 {
color: #42b983;
}
</style>
2. 组件的混入(Mixins)
// 定义一个混入对象
var myMixin = {
created: function() {
console.log('混入对象的钩子被调用');
},
data: function() {
return {
mixinData: '这是混入对象中的数据'
};
},
methods: {
mixinMethod() {
console.log('这是混入对象中的方法');
}
}
};
// 使用混入对象的组件
var app = new Vue({
el: '#app',
mixins: [myMixin],
created: function() {
console.log('组件的钩子被调用');
},
data: function() {
return {
localData: '这是组件中的数据'
};
},
methods: {
localMethod() {
console.log('这是组件中的方法');
}
}
});
四、Vue.js的优点与缺点
Vue.js的优点
Vue.js的缺点