赞
踩
//并且在在vite.config.js中配置
plugins: [
vue(
{ reactivityTransform: true }
),
AutoImport(
{
imports:['vue','vue-router']
}
)
]
父组件
<template>
<div>
<div>
<h1>父组件</h1>
<input type="text" v-model="valueParent" />
</div>
<A v-model:valueParent="valueParent" />
<B v-model:valueParent="valueParent" />
</div>
</template>
<script setup>
import A from "./a.vue";
import B from "./b.vue";
let valueParent = ref("父组件初始值");
,
</script>
<style>
</style>
子组件A
<template>
<div>
<h1>子组件A</h1>
<input type="text" v-on:input="changeValue" v-model="valueParent" />
</div>
</template>
<script setup>
defineProps({
valueParent: {
type: String,
default: "aaa",
},
});
let emit = defineEmits(["update:valueParent"]);
const changeValue = (event) => {
console.log(event);
emit("update:valueParent", event.target.value);
};
</script>
<style>
</style>
子组件B
<template>
<div>
<h1>子组件B</h1>
<input type="text" v-on:input="changeValue2" v-model="valueParent" />
</div>
</template>
<script setup>
defineProps({
valueParent: {
type: String,
default: "aaa",
},
});
let emit = defineEmits(["update:valueParent"]);
const changeValue = (event) => {
console.log(event);
emit("update:valueParent", event.target.value);
};
</script>
<style>
</style>
OS:其实这里我可以开启Minix混入的,不熟练,还是再来一遍。
先新建一个中转文件 xxxxx.js(随便取名)
import mitt from 'mitt'
const emitter=mitt()
export default emitter
A组件
import emitter from "./xxxx.js";
let strA = ref("这是A组件的数据");
const btn = () => {
emitter.emit("fn", strA);
};
B组件
import emitter from "./xxxx.js";
let strB = ref("");
onBeforeMount(() => {
emitter.on("fn", (e) => {
console.log(e.value);
strB.value = e.value;
});
});
import { createRouter, createWebHistory } from "vue-router"
import Home from "../views/home/index.vue"
console.log(Home)
const routes = [
{
path: "/home",
name: "Home",
component:Home
},
{
path: "/about",
name: "About",
component:()=>import('../views/about/index.vue')
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
export default router
import { createApp } from 'vue'
import './style.css'
import router from './router/index'
import App from './App.vue'
const app = createApp(App)
app.use(router)
app.mount("#app")
<div>
//!!!!!!!不然你会看不到界面(同时也要在url导航栏输入对应地址!!!!!)
<router-view></router-view>
</div>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。