请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

vue3 this问题

老师,setup中不能使用this,那我要通过什么方式获取this.$parent呢

正在回答 回答被采纳积分+3

插入代码

2回答

张轩 2021-05-29 10:39:53

必须保证 父组件的 props 是有值的才可以,你这里有可能父组件没有传这个 props,以下两种方式都可以

1
2
console.log(instance?.proxy?.$parent?.$props)
console.log(instance?.parent?.props)

获得父组件的 props 

0 回复 有任何疑惑可以回复我~
张轩 2021-05-29 09:46:30

同学你好 可以通过 getCurrentInstance() 获得,这个内容稍微有点难度。

1
2
3
4
5
6
7
8
// 拿到一个vue 提供的内部方法
import { getCurrentInstance } from 'vue'
// 在setup 中添加如下代码
setup() {
    const instance = getCurrentInstance()
    // 可以通过这个 intance 上面的属性拿到一些原来 this 上面的值
    console.log(instance?.proxy?.$parent)
}


0 回复 有任何疑惑可以回复我~
  • 提问者 慕盖茨7486032 #1
    老师,还是不行,我想获取parent上的props中的rules属性,但是任然是proxy对象,并且target中没有rules的值, instance?.proxy?.$parent?.$props.rules
    回复 有任何疑惑可以回复我~ 2021-05-29 10:05:27

相似问题

登录后可查看更多问答,登录/注册

问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号