简体   繁体   English

Vue-路由器动态组件

[英]Vue-Router Dynamic Components

I'm trying to create a single page vue.js application using vue-router. 我正在尝试使用vue-router创建一个单页vue.js应用程序。 The default route (/) should be a list of links to the other pages (like a landing page). 默认路由(/)应该是指向其他页面(如登录页面)的链接的列表。 So far, all the other pages are dialogs for different scenarios. 到目前为止,所有其他页面都是针对不同情况的对话框。 I want to make creating new dialogs as straight forward as possible, so I've created a base-dialog page which sets up the generic dialog look, while leaving the details up to the specific route. 我希望尽可能直接地创建新对话框,因此我创建了一个基本对话框页面,该页面设置了通用对话框外观,同时将详细信息留给了特定途径。 There are three sections: basic settings, logging settings, and advanced settings. 分为三个部分:基本设置,日志记录设置和高级设置。 Basic settings will be required for each scenario, logging settings is the same for each scenario, and advanced settings are optional. 每个方案都需要基本设置,每个方案的日志记录设置都相同,而高级设置是可选的。 I'm using named router-views to load basic and advanced settings. 我正在使用命名的路由器视图来加载基本和高级设置。


Previous Research 先前的研究

I've based my solution on this thread . 我的解决方案基于此线程


Code Samples 代码样例

DialogPage.vue DialogPage.vue

 <template> <div> <div id="basic-options" class="options"> <router-view name="basicView" basic="true"/> </div> <div id="logging-options" class="alt-options"> <!-- Custom components needed for logging options. This renders properly. --> </div> <div id="advanced-options" class="alt-options"> <router-view name="advancedView" basic="false"/> </div> </div> </template> 

{Scenario}.vue {Scenario} .vue

 <template> <div> <!-- custom components to set up basic dialogs for {Scenario} --> </div> </template> 

{Scenario}Advanced.vue is the same as {Scenario}.vue except using different children in the div. {Scenario} Advanced.vue与{Scenario} .vue相同,只不过在div中使用了不同的子级。

ScenarioSelector.js ScenarioSelector.js

 /* Import Scenario Here */ import ScenarioA from '@/components/Scenarios/A' import ScenarioAAdvanced from '@/components/Scenarios/AAdvanced' import ScenarioB from '@/components/Scenarios/B' import ScenarioBAdvanced from '@/components/Scenarios/BAdvanced' /* Add them to the list */ const components = { ScenarioA, ScenarioAAdvanced, ScenarioB, ScenarioBAdvanced } /* Don't change anything here */ export default { functional: true, props: [ { name: 'scenario', type: String }, { name: 'basic', type: Boolean, default: true } ], render (createElement, context) { var scenario = context.props.scenario console.log('Log anything, please') if (context.props.basic) { scenario += 'Advanced' } return createElement(components[scenario], context.data, context.children) } } 

Relevant section of router/index.js router / index.js的相关部分

 export default new Router({ routes: [ /* Other routes */, { path: "/dialog/:scenario", component: DialogPage, props: true, children:[ { path: "*", components: { basicView: ScenarioSelector, advancedView: ScenarioSelector }, props: { basicView: true, advancedView: true } } ] } ] } 


The Problem 问题

It doesn't seem to be entering the render function of ScenarioSelector . 它似乎没有进入ScenarioSelectorrender功能。 Nothing gets logged. 什么都没有记录。 I have gotten it to enter that function once or twice, but I'm not sure what I did differently to make that happen, and even when it does happen there are no props being set. 我已经知道它要进入该函数一次或两次,但是我不确定我做了什么不同的工作来实现该功能,即使发生了,也没有设置任何道具。


Other Things I've Tried Alternate routes: 我尝试过的其他方法替代路线:

 export default new Router({ routes: [ /* Other routes */, { path: "/dialog/", component: DialogPage, props: true, children:[ { path: "/:scenario", components: { basicView: ScenarioSelector, advancedView: ScenarioSelector }, props: { basicView: true, advancedView: true } } ] } ] } 

For this one, the links from the landing page (which are just /dialog/{Scenario}) don't do anything. 对于这一点,登录页面中的链接(只是/ dialog / {Scenario})没有任何作用。

 export default new Router({ routes: [ /* Other routes */, { path: "/dialog/", component: DialogPage, props: true, children:[ { path: "ScenarioA", components: { basicView: ScenarioA, advancedView: ScenarioAAdvanced } }, { path: "ScenarioB", components: { basicView: ScenarioB, advancedView: ScenarioBAdvanced } } ] } ] } 

Again, in this case the links do nothing. 同样,在这种情况下,链接不执行任何操作。


Edit 编辑

I mentioned earlier in the post that I wasn't sure what I'd done differently to make that code execute. 我在帖子的前面提到过,我不确定为使该代码执行而做了什么不同的工作。 I just changed * to / in the child path, and it executes. 我只是在子路径中将*更改为/ ,然后执行。 Sample below: 示例如下:

 export default new Router({ routes: [ /* Other routes */, { path: "/dialog/:scenario", component: DialogPage, props: true, children:[ { path: "/", components: { basicView: ScenarioSelector, advancedView: ScenarioSelector }, props: { basicView: true, advancedView: true } } ] } ] } 

I have just got this working properly. 我刚刚使它正常工作。 There were two issues. 有两个问题。

  1. This one I already mentioned in my edited. 我已经在编辑中提到了这一点。 I should have used "/" as the path for the children, rather than "*" . 我应该使用"/"作为孩子的路径,而不是"*" I thought "*" would have been the safer choice, since it wouldn't matter what the last part of the path was, but it seems to not work (if anyone could elaborate on why, that would be greatly appreciated). 我认为"*"将是更安全的选择,因为路径的最后部分是什么并不重要,但是它似乎不起作用(如果有人能详细说明原因,将不胜感激)。 Please see the Edit section of the question for the relevant code section. 有关相关代码部分,请参见问题的“ 编辑”部分。
  2. The second issue was my prop definition in ScenarioSelector.js . 第二个问题是我在ScenarioSelector.js prop定义。 The answer to this question explains what I did wrong. 这个问题的答案解释了我做错了什么。 If I wanted to use prop validation, my props should have been an object. 如果我想使用道具验证,那么我的props应该是一个对象。 Relevant code sample below: 相关代码示例如下:

 /* ... */ export default { functional: true, props: { scenario: String, basic: { type: Boolean, default: true } }, render (createElement, context) { var scenario = context.props.scenario console.log('Log anything, please') if (context.props.basic) { scenario += 'Advanced' } return createElement(components[scenario], context.data, context.children) } } 

After fixing both of these issues, the props values are bound correctly and the log is outputted to the dev tools console. 解决了这两个问题之后,正确绑定了props值,并将日志输出到dev工具控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM