<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 16:07:25 浏览: 91
<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>
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<van-nav-bar> <template v-slot:right> 去注册 > </template> </van-nav-bar> 飞腾兔 国内领先的人力资源服务器 <van-field v-model="form.phoneNumber" clearable placeholder="11位手机号" class="custom-field" > <template #label> +86 <van-divider vertical /> </template> </van-field> <van-field v-model="form.idNumber" clearable placeholder="后六位" class="custom-field" > <template #label> 身份证 <van-divider vertical /> </template> </van-field> <van-button type="primary" class="btn" @click="getLogin"> 登 录 </van-button> <van-button @click="getRegister">身份证登录</van-button> 登录代表您已同意用户协议和隐私协议 给用户协议,加一个click事件,点击出现一个dialog,里面有协议内容
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<template> <input type="checkbox" v-model="item.selected" />
{{ item.title }} ¥{{ item.price }}<button @click="remove(index)">-</button>{{ item.plenty }}<button @click="add(index)">+</button>删除 总计不含运费
已勾选商品 {{ count }} 件,总价¥4496元 <button>去结算</button> </template> <script> export default { data() { return { count: 1, lists: [ { selected: true, plenty: 1, title: "小米(Mi)小米Note 16G双网通版", price: 2199, src: require("../assets/image/a15.jpg") } , { selected: false, plenty: 0, title: "小米(Mi)小米Note 16G双网通版", price: 2199, src: require("../assets/image/a15.jpg") }]} }, methods: { remove(index) { if (this.lists[index].plenty > 0) { this.lists[index].plentythis.lists[index].plenty - 1}}, add(index) { if (!this.lists[index].plenty >= 0) { this.lists[index].plenty = this.lists[index].plenty + 1 } } } }; </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)