[英]I don't know how to organize a web app that uses component based web framework
I am trying to learn component based frameworks for frontend apps. 我正在尝试为前端应用程序学习基于组件的框架。 Currently, I am using RiotJS but it applies to any framework that uses the same concepts (React, Angular 2.0 etc).
目前,我使用的是RiotJS,但它适用于任何使用相同概念的框架(React,Angular 2.0等)。
In a basic MVC frontend frameworks (eg AngularJS), the controllers and router were very connected to each other. 在基本的MVC前端框架(例如AngularJS)中,控制器和路由器彼此非常连接。 But with a component based framework, the line between router and controllers is much wider.
但是使用基于组件的框架,路由器和控制器之间的界限要宽得多。 And this is what confuses me the most.
而这最让我困惑的是。
Here is one example of an app that I am trying to build: 以下是我要构建的应用程序的一个示例:
I have three main UI elements: Navigation Bar, Content Area, and Signin Form. 我有三个主要UI元素:导航栏,内容区域和登录表单。 So, I created three components: my-navbar, my-content, my-signin.
所以,我创建了三个组件:my-navbar,my-content,my-signin。 I was able to create multiple routes per component.
我能够为每个组件创建多个路由。 So for example, if there is a route changes, the navbar updates the active "module."
因此,例如,如果路线发生变化,导航栏将更新活动的“模块”。 Making this was easy because all I am doing is changing class of a list item.
这很简单,因为我所做的只是更改列表项的类。
Now, I want to load other tags inside <my-content></my-content>
. 现在,我想在
<my-content></my-content>
加载其他标签。 In AngularJS, I was always changing the view completely (using ui-router). 在AngularJS中,我总是完全改变视图(使用ui-router)。 How can I achieve that in a component based framework.
如何在基于组件的框架中实现这一点。 Let's say that I have 2 more components called my-content-users-list-view, my-content-users-detail-view.
假设我还有两个名为my-content-users-list-view,my-content-users-detail-view的组件。 How can I add them to the component
my-content
based on the route? 如何根据路线将它们添加到组件
my-content
? Do I just add it like document.innerHTML += '<my-content-users-list-view></my-content-users-list-view>
? 我只是添加它像
document.innerHTML += '<my-content-users-list-view></my-content-users-list-view>
?
I know most of my syntax is RiotJS but I will understand it if you write it in another framework's syntax. 我知道我的大部分语法都是RiotJS,但是如果你用另一个框架的语法编写它我会理解它。
Thank you! 谢谢!
Essentially, yes, you could just append your tag as a DOM node and then call Riot to mount it: 基本上,是的,您可以将您的标记作为DOM节点附加,然后调用Riot来安装它:
riot.route('/awesome-route', () => {
const tag = 'your-awesome-tag';
const options = { ... };
const elem = document.createElement(tag);
// TODO empty your content container using pure DOM or jQuery to get rid of the previous route's view...
document.querySelector('#content').appendChild(elem);
riot.mount(elem, tag, options);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.