<template> <div>我是孙组件</div> <button @click="sendMoneytoYe">我给爷爷传3000</button> </template> <script> // 注入 接到爷爷给的钱 import { inject } from 'vue' export default { name: 'GrandSon', setup() { // 接收爷爷的空的函数 const moneySendGrandson = inject('moneySendGrandson') const sendMoneytoYe = function() { moneySendGrandson(3000) } return { sendMoneytoYe } } } </script> 把这段改成<script setup lang="ts">
时间: 2023-05-19 19:07:25 浏览: 87
<template>
<div>我是孙组件</div>
<button @click="sendMoneytoYe">我给爷爷传3000</button>
</template>
<script setup lang="ts">
import { inject } from 'vue'
export default {
name: 'GrandSon',
setup() {
// 接收爷爷的空的函数
const moneySendGrandson = inject<() => void>('moneySendGrandson')
const sendMoneytoYe = function() {
moneySendGrandson(3000)
}
return { sendMoneytoYe }
}
}
</script>
相关问题
<template > <div class="product-wrap" > <div class="product" v-for="product in products" :key="product.id"> <img :src="product.image" alt="" @click="handleClick"> <div> <span>{{ product.name }}</span> <h4>¥{{ product.price }}</h4> </div> </div> </div> </template> <script> <template> <div class="container"> <div class="detail-wrap"> <img :src="imgSrc" alt=""> <div> <h1>{{ msg }}</h1> <p><span>¥</span><span>{{ price }}</span></p> <div><input type="number" v-model="num"></div> <button v-on:click="addToCart">加入购物车</button> </div> </div> </div> </template> <template> <div class="container"> <div class="detail-wrap"> <img :src="imgSrc" alt=""> <div> <h1>{{ msg }}</h1> <p><span>¥</span><span>{{ price }}</span></p> <div><input type="number" v-model="num"></div> <button v-on:click="addToCart">加入购物车</button> </div> </div> </div> </template>我要显示goodslist做主页,点击主页goodslist图片列表实现跳转到其余两页,vue.app如何实现,请示用动态组件实现
你可以在主页中使用动态组件来实现点击图片列表跳转到其余两页的功能,具体实现步骤如下:
1. 在主页中创建一个包含三个按钮的列表,分别对应三个组件(包括goodslist组件和另外两个组件)。
2. 使用Vue Router来实现页面跳转功能,配置路由表,并将三个组件分别对应到不同的路由路径。
3. 在主页中使用动态组件<router-view>,并通过路由跳转来实现展示不同的组件。
以下是示例代码:
```
<template>
<div>
<ul>
<li @click="toGoodsList">跳转到GoodsList组件</li>
<li @click="toComponent1">跳转到Component1组件</li>
<li @click="toComponent2">跳转到Component2组件</li>
</ul>
<router-view></router-view>
</div>
</template>
<script>
import GoodsList from './GoodsList.vue'
import Component1 from './Component1.vue'
import Component2 from './Component2.vue'
export default {
components: {
GoodsList,
Component1,
Component2
},
methods: {
toGoodsList() {
this.$router.push('/goodslist')
},
toComponent1() {
this.$router.push('/component1')
},
toComponent2() {
this.$router.push('/component2')
}
}
}
</script>
```
在路由表中,你需要定义三个路由路径,分别对应三个组件:
```
import GoodsList from './GoodsList.vue'
import Component1 from './Component1.vue'
import Component2 from './Component2.vue'
const routes = [
{ path: '/goodslist', component: GoodsList },
{ path: '/component1', component: Component1 },
{ path: '/component2', component: Component2 }
]
export default new VueRouter({
routes
})
```
当你点击列表中的某个选项时,路由会根据对应的路径跳转到相应的组件,并在<router-view>中渲染该组件。
<template> <div class="container"> <div class="detail-wrap"> <img :src="imgSrc" alt=""> <div> <h1>{{ msg }}</h1> <p><span>¥</span><span>{{ price }}</span></p> <div><input type="number" v-model="num"></div> <button v-on:click="addToCart">加入购物车</button> </div> </div> </div> </template>这是GoodsDetail01,<template> <div class="container"> <div class="detail-wrap"> <img :src="imgSrc" alt=""> <div> <h1>{{ msg }}</h1> <p><span>¥</span><span>{{ price }}</span></p> <div><input type="number" v-model="num"></div> <button v-on:click="addToCart">加入购物车</button> </div> </div> </div> </template>这是GoodsDetail02,这是<template > <div class="product-wrap" > <div class="product" v-for="product in products" :key="product.id" @click="handleClick(product.id)"> <img :src="product.image" alt="" > <div> <span>{{ product.name }}</span> <h4>¥{{ product.price }}</h4> </div> <component :is="currentComponent"></component> </div> </div> </template>这是goodslist商品主页,实现goodslist点击跳转到GoodsDetail01和GoodsDetail02,请用动态组件实现跳转,写出vue.app和goodslist代码
Vue.app代码如下:
```html
<template>
<div id="app">
<router-view></router-view>
</div>
</template>
<script>
export default {
name: "App",
};
</script>
```
goodslist代码如下:
```html
<template>
<div>
<h2>商品列表</h2>
<div class="product-wrap">
<div class="product" v-for="product in products" :key="product.id" @click="handleClick(product.id)">
<img :src="product.image" alt="">
<div>
<span>{{ product.name }}</span>
<h4>¥{{ product.price }}</h4>
</div>
</div>
</div>
<component :is="currentComponent"></component>
</div>
</template>
<script>
import GoodsDetail01 from "./GoodsDetail01.vue";
import GoodsDetail02 from "./GoodsDetail02.vue";
export default {
name: "GoodsList",
components: {
GoodsDetail01,
GoodsDetail02,
},
data() {
return {
products: [
{
id: 1,
name: "商品1",
price: 100,
image: "https://via.placeholder.com/150",
},
{
id: 2,
name: "商品2",
price: 200,
image: "https://via.placeholder.com/150",
},
],
currentComponent: "",
};
},
methods: {
handleClick(id) {
if (id === 1) {
this.currentComponent = "GoodsDetail01";
} else if (id === 2) {
this.currentComponent = "GoodsDetail02";
}
},
},
};
</script>
```
阅读全文