const clickTitle = id => { school(id).then(res => { redirect.schoolDetails(res.school) }) }uniapp的vue3写法父页面请求的res.school将这个数据传入schoolDetails这个子页面中 我如何在子页面获取到res.school的数据呢
时间: 2024-03-27 20:37:04 浏览: 18
你可以在子页面中使用props来接收父页面传递过来的数据。具体实现方法如下:
1. 在父页面中定义一个变量,将res.school赋值给这个变量,然后通过props将这个变量传递给子页面。
```vue
<template>
<div>
<child :school="school"></child>
</div>
</template>
<script>
import Child from '@/components/Child.vue'
export default {
components: {
Child
},
data() {
return {
school: ''
}
},
mounted() {
clickTitle(id).then(res => {
this.school = res.school
})
}
}
</script>
```
2. 在子页面中通过props接收父页面传递过来的数据。
```vue
<template>
<div>
<p>{{ school }}</p>
</div>
</template>
<script>
export default {
props: {
school: {
type: String,
required: true
}
}
}
</script>
```
这样,子页面就可以获取到父页面传递过来的res.school的数据了。
相关问题
修改代码 const add=()=>{ axios.post("/shelf/add",{ user_id:localStorage.getItem("id"), book_id:route.query.bookId }).then((res)=>{ const data = res.data console.log(data) }) }
这段代码是一个使用 axios 发送 POST 请求的函数,请求地址是 `/shelf/add`,请求参数是当前用户的 id 和书本的 id。当请求成功后,会将响应数据打印到控制台。如果要修改这个函数,可以根据实际需求修改请求地址和请求参数,如下所示:
```javascript
const add = () => {
axios.post("/api/add-to-cart", {
userId: localStorage.getItem("userId"),
productId: route.query.productId,
quantity: 1
}).then((res) => {
const data = res.data;
console.log(data);
});
};
```
这个例子中,我们将请求地址改为了 `/api/add-to-cart`,请求参数包括了用户的 id、产品的 id 和购买数量,用于向购物车添加商品。你可以根据具体的业务需求修改这些参数。
const unread = ref([]) const getUnRead = () => { request.get('/messages/unread').then(res => { unread.value = res.data }) }
这段代码使用了 Vue 3 的 Composition API,其中使用了 ref 函数创建一个响应式变量 unread,并将其初始化为空数组。另外,定义了一个函数 getUnRead,用于获取未读消息,并将获取到的数据赋值给 unread 变量。
在这段代码中,使用了 request.get 方法发送 HTTP GET 请求,请求的地址为 '/messages/unread',并将获取到的数据赋值给 unread 变量。这意味着,当执行 getUnRead 函数时,如果请求成功,unread 变量的值将被更新为最新的未读消息列表。