导航

跟常规的 .vue 组件没什么区别,就是个对象,样式 import 引入就好
上边的 MyLogo 对象,其实就是平常 .vue 汇总的 export default 对象
为了将上边 index.js 、index.scss 整合到一个文件里(.vue)边去。
分为
浏览器没法解析这种文件
让 vue-loader 去识别最后打包成类似上边 MyLogo/index.js 的文件
导入时无需写 .vue 后缀

vue-loader 解析 .vue 文件
vue-style-loader 解析 css
var app = new Vue({});
app.component('my-search', {
data() {
return {
placeholder: 'Please type the keyword...',
keyword: ''
}
},
template: `
<div style="float: left;">
<input
type="text"
:placeholder="placeholder"
v-model="keyword" />
<button @click="searchAction">Search</button>
</div>
`,
methods: {
searchAction() {
console.log(this.keyword);
}
}
});
注册后就不用在各文件中注册了
全局组件放到 global 中去:

导入:

kebab-case<tab-nav></tab-nav>
<!-- 父组件 -->
<tab-nav
:navData="navData"
:tabIndex="tabIndex"
/>
<!-- 子组件 -->
props: ['navData', 'tabIndex'],
data() {
return {
tabIndex: this.tabData.initialIndex
}
},
props: ['tabData'],
computed: {
navData() {
return this.tabData.data.map(item => { id: item.id, title: item.title });
},
pageData() {}
}
@自定义事件名称="函数名"this.$emit('自定义事件名称', ...传参)子组件:
<div
@click="changeIndex(myIndex)"><div>
export default {
methods: {
changeIndex(index) {
this.$emit('changeIndex', index);
}
}
}
父组件:
<nav-item
...
@changeIndex="changIndex"
/>
export default {
methods: {
changIndex(index) {
console.log(index);
// 再向上传递
this.$emit('changeIndex', index);
}
}
}
子组件
<template>
<div class="tab-search">
<input
type="text"
v-model="value" />
</div>
</template>
<script>
export default {
name: 'tab-search',
props: ['searchValue'],
// 第一种方案:
data() {
return {
value: this.searchValue
}
},
watch: {
value() {
// 要不要 update: 都行,就是个普通自定义事件 例如写成 updateSearchValue 也行
this.$emit('update:searchValue', this.value);
}
}
}
</script>
<style lang="scss">
.tab-search {
height: 50px;
padding: 6px 10px;
border-bottom: 1px solid #000;
box-sizing: border-box;
input {
width: 100%;
height: 38px;
box-sizing: border-box;
}
}
</style>
父组件
<tab-search
:searchValue="searchValue"
@update:searchValue="updateSearchValue"
/>
export default {
methods: {
updateSearchValue(value) {
this.tabData.data.some((item, index) => {
if (item.title.includes(value) || item.content.includes(value)) {
this.tabIndex = index;
return true;
}
});
}
}
}
子组件
<template>
<div class="tab-search">
<input
type="text"
v-model="value" />
</div>
</template>
<script>
export default {
name: 'tab-search',
props: ['searchValue'],
// 第二种方案:
computed: {
value: {
get() {
// 直接返回父组件传递的值
return this.searchValue
},
set(newValue) {
// 这里不需要 this.value = newValue
// 直接传递事件让外边改,value就自动改了
this.$emit('update:searchValue', newValue);
}
}
}
}
</script>
<style lang="scss">
.tab-search {
height: 50px;
padding: 6px 10px;
border-bottom: 1px solid #000;
box-sizing: border-box;
input {
width: 100%;
height: 38px;
box-sizing: border-box;
}
}
</style>
父组件
<tab-search
:searchValue="searchValue"
@update:searchValue="updateSearchValue"
/>
export default {
methods: {
updateSearchValue(value) {
this.tabData.data.some((item, index) => {
if (item.title.includes(value) || item.content.includes(value)) {
this.tabIndex = index;
return true;
}
});
}
}
}
子组件
<template>
<div class="my-form">
<div class="input-box">
<input type="text"
v-model="username"
placeholder="Username"
>
</div>
<div class="input-box">
<input type="text"
v-model="password"
placeholder="Password"
>
</div>
</div>
</template>
<script>
export default {
name: 'my-form',
props: ['myUsername', 'myPassword'],
computed: {
username: {
get() {
return this.myUsername; // 获取父组件传递的prop最新值
},
set(newVal) {
this.$emit('update:myUsername', newVal); // 告知父组件得更新prop值了
}
},
password: {
get() {
return this.myPassword;
},
set(newVal) {
this.$emit('update:myPassword', newVal);
}
}
}
}
</script>
<style>
</style>
父组件
<template>
<div>
<my-form
:myUsername="myUsername"
:myPassword="myPassword"
@update:myUsername="updateMyUsername"
@update:myPassword="updateMyPassword"
></my-form>
</div>
</template>
<script>
import MyForm from './components/MyForm';
export default {
name: "App",
components: {
MyForm
},
data() {
return {
modalShow: true,
myUsername: '',
myPassword: ''
}
},
methods: {
updateMyUsername(val) {
this.myUsername = val;
},
updateMyPassword(val) {
this.myPassword = val;
}
}
};
</script>
<style></style>
父组件 TodoHeader
<template>
<div>
<todo-input
...
ref="todoInput"
></todo-input>
...
</div>
</template>
<script>
...
export default {
...
addTodo() {
...
this.$refs.todoInput.reset();
}
}
}
</script>
子组件 TodoInput
<template>
<input
type="text"
:placeholder="placeholder"
v-model="content" />
</template>
<script>
export default {
...
methods: {
reset() {
this.content = '';
}
}
}
</script>
浏览器中 table 不能嵌套 div、vue 组件,div 或者组件东西能出来,但是在 table 标签外边。

要想解决此问题,可以显示的告诉vue我这是个组件:

父组件使用:
<modal
...
>
<my-form></my-form>
</modal>
子组件: