Vue JS rc-1 通过道具传递数据不起作用 [英] Vue JS rc-1 Passing Data Through Props Not Working

查看:17
本文介绍了Vue JS rc-1 通过道具传递数据不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Vue 1.0.0-rc.1 的发行说明中,我们被告知

<块引用>

"继承选项已被弃用.始终将数据传递给子项组件通过道具."

但是,组件 API 部分说

<块引用>

$data 不能再用作道具."

我一直在尝试将数据传递给我的根 Vue 实例的子组件,但没有任何运气.

在 0.12.* 版本中,如果您想要/需要访问父实例的数据、方法等,您只需添加...

继承:true

...到子组件.

现在,在尝试通过 props 访问父数据时,我继续撞墙.这是一个简化的例子:

app.js:

new Vue({el: '#app',数据: {授权:假,当前视图:'欢迎视图'},成分: {'欢迎视图':要求('./views/welcome')}});

views/welcome.js:

module.exports = {道具:['授权'],模板:需要('./welcome.template.html')};

views/welcome.template.html:

<div v-if="authorized"><p>您已登录</p></div><div v-else>请登录</div>

主视图文件(app.blade.php)

<代码>...<body id="应用程序"><component :is="currentView"></component>...

已授权"道具根本无法通过这种方式识别.它可以在组件外部(在应用程序"ID 中)正常工作,但不能在模板中工作.

目前,我可以在任何需要的地方使用 $root 来访问我需要的数据.例如:

<div v-if="$root.authorized"><p>您已登录</p></div>

但是,我的理解是这到处都是糟糕的形式",作为文档说:

<块引用>

虽然可以访问父链的任何实例,但您应该避免直接依赖子组件中的父数据和更喜欢使用 props 显式向下传递数据.

所以,我需要知道的是...我如何显式地使用道具?我显然以错误的方式处理它,因为如果我只是将它们列在 'props: []' 数组中,它们对我的子组件不可用.我在这里缺少什么?

归根结底,重构我当前的代码以替换inherit: true"并且仍然可以访问根实例数据和函数的最佳方法(标准和实践)是什么?任何有关这方面的帮助/建议将是最受欢迎的.提前致谢.

解决方案

请参阅此页面上 @StephenHallgren 的回答,了解访问 HTML 中道具的正确方法.

至于它的其余部分,(如何正确重构代码以替换 'inherit:true',我在这里包括 我在 官方 Vue 论坛,以防其他人将来遇到此问题.

他对上述问题的回答是:

<块引用>

如果你对结构相当确定,你可以使用$root.authorized.

或者,根本不要将授权状态放在根目录中.有用户状态的专用模块,可以以任何方式导入成分.看http://rc.vuejs.org/guide/application.html#State_Management

我的结论是 - 如果存在不会改变的具体全局变量,并且应用程序结构合理,则可以使用 $root(或 $parent 视情况而定),并且 - 元素的状态有时会发生变化(例如用户是否获得授权/登录),关键是使用状态管理模块.>

同时,在父子之间传递 props 时,必须在 props 数组中声明 props,然后将它们绑定到 HTML 中的组件.

例如...

app.js:

new Vue({el: '#app',数据: {授权:假,当前视图:'欢迎视图'},成分: {'欢迎视图':要求('./views/welcome')}});

views/welcome.js:

module.exports = {道具:['授权'],模板:需要('./welcome.template.html')}

welcome.template.html:

<div v-if="authorized"><p>您已登录</p></div><div v-else>请登录</div>

主 HTML

<component :is="currentView" v-bind:authorized="authorized"></component>

(或简写)

<component :is="currentView" :authorized="authorized"></component>

In the release notes for Vue 1.0.0-rc.1, we are told

"The inherit option has been deprecated. Alway pass data to child components via props."

However, the Component API section says

"$data can no longer be used as a prop."

I have been trying to pass data to child components of my root Vue instance, and have had no luck whatsoever.

In version 0.12.*, if you want/need access to a parent instance's data, methods, etc., you would simply add...

inherit: true

...to a child component.

Now, in attempting to access the parent data via props, I continue to hit a brick wall. Here is a simplified example:

app.js:

new Vue({

    el: '#app',

    data: {
        authorized: false,
        currentView: 'welcome-view'
    },

    components: {
        'welcome-view': require('./views/welcome')
    }
});

views/welcome.js:

module.exports = {

    props: ['authorized'],

    template: require('./welcome.template.html')
};

views/welcome.template.html:

<div v-if="authorized"><p>You Are Logged In</p></div>
<div v-else>Please Log In</div>

Main View File (app.blade.php)

...
  <body id="app">
      <component :is="currentView"></component>
  </body>
...

The 'authorized' prop is not recognized at all this way. It works outside of the component (within the "app" id) just fine, but not within the template.

At the moment, I can access the data I need by using $root everywhere I need it. For instance:

<div v-if="$root.authorized"><p>You Are Logged In</p></div>

But, my understanding is that this is 'bad form' all around, as the docs say:

Although it’s possible to access any instance the parent chain, you should avoid directly relying on parent data in a child component and prefer passing data down explicitly using props.

So, what I need to know is... how can I explicitly use props? I am clearly going about it the wrong way, since they are not available to my child components if I just list them in the 'props: []' array. What am I missing here?

At the end of the day, what is the best way (standards and practices) to refactor my current code to replace 'inherit: true', and still have access to the root instance data and functions? Any help/advice on this would be most welcome. Thanks in advance.

解决方案

See @StephenHallgren's answer on this page for the correct way to access props in the HTML.

As for the rest of it, (how to properly refactor code to replace 'inherit:true', I am including here the answer I received from Evan Y. on the official Vue forum, in case anyone else runs across this in the future.

His answer to the question posed above was:

If you are fairly certain about the structure, you can use $root.authorized.

Alternatively, don't put the authorized state in the root at all. Have a dedicated module for user state that can be imported in any component. See http://rc.vuejs.org/guide/application.html#State_Management

My take-away from this is that - where there are concrete, global variables that will not change, and the app structure is sound, it is okay to use $root (or $parent as the case may be), and - where elements have state that will sometimes change (such as whether or not a user is authorized/logged in), the key is to use a state management module.

Meanwhile, when passing down props between parent and child, one must declare the props in the props array, then bind them to the component in the HTML.

For example...

app.js:

new Vue({

    el: '#app',

    data: {
        authorized: false,
        currentView: 'welcome-view'
    },

    components: {
        'welcome-view': require('./views/welcome')
    }
});

views/welcome.js:

module.exports = {
    props: ['authorized'],
    template: require('./welcome.template.html') 
}

welcome.template.html:

<div v-if="authorized"><p>You Are Logged In</p></div>
<div v-else>Please Log In</div>

main HTML

<body id="app">
    <component :is="currentView" v-bind:authorized="authorized"></component>
</body>

(or shorthand)

<body id="app">
    <component :is="currentView" :authorized="authorized"></component>
</body>

这篇关于Vue JS rc-1 通过道具传递数据不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆