对于想了解Angular2设置默认路由的读者,本文将提供新的信息,我们将详细介绍angular配置路由,并且为您提供关于Angular2在路由器插座中设置默认内容、angular的select中ng-
对于想了解Angular2设置默认路由的读者,本文将提供新的信息,我们将详细介绍angular配置路由,并且为您提供关于Angular 2在路由器插座中设置默认内容、angular 的 select 中 ng-options 设置默认值、Angular.json - 为测试设置默认配置、angular1.x和angular2+并行,angular1.x 升级 angular2+方案的有价值信息。
本文目录一览:- Angular2设置默认路由(angular配置路由)
- Angular 2在路由器插座中设置默认内容
- angular 的 select 中 ng-options 设置默认值
- Angular.json - 为测试设置默认配置
- angular1.x和angular2+并行,angular1.x 升级 angular2+方案
Angular2设置默认路由(angular配置路由)
这是路由模块
const appRoutes: Routes = [ { path: 'login',component: LoginComponent,useAsDefault:true },//returns an error { path: 'dash',redirectTo:"dashboard" },{ path: 'reset-password',component: ResetPasswordComponent },{ path: '',redirectTo: '/dashboard',pathMatch: 'full' },{ path: '**',component: PageNotFoundComponent } ];
以上返回错误
LoginComponent; useAsD...' is not assignable to type 'Route[]'
可能有什么不对
解决方法
所以,不需要使用AsDefault,你可以简单地给Login As Default Routes.And,因为我看到没有为Dashboard导入的组件,所以,
const appRoutes: Routes = [ { path: '',redirectTo: "/login",pathMatch: 'full' },{ path: 'login',component: LoginComponent },component: PageNotFoundComponent } ];
Angular 2在路由器插座中设置默认内容
我有一个父组件,将在该页面上的路由器插座中显示子路由组件.
单击时,有2个按钮将使用子组件填充路由器插座.如何将内容放置在路由器插座内部,例如. “单击选项以显示数据”,然后单击该按钮后此消息将清除,子组件将显示在路由器插座中?
<div> <div> <divrouterLink="/unpaid/people"> View People </div> <divrouterLink="/unpaid/bills"> View Bills </div> </div> </div> <router-outlet>Click an options to display the data</router-outlet>
编辑
这是我的路线
path: 'unpaid',component: UnpaidBillsComponent,children: [ {path: 'people',component: UnpaidPeopleComponent},{path: 'bills',component: UnpaidBillListComponent} ]
解决方法
default text inside the router-outlet until it is populated
首先,你的假设是错误的,内容不是插在插座内,而是插在插座下面.
您无法在模板中设置默认内容,但可以设置包含默认内容的默认路由.只需使用路径:”并使用带有“默认”模板的组件.
使用您需要的“默认”模板创建组件:
@Component({template: `Default template here`}) export class DefaultTemplateComponent {}
并将其添加到您的路线.
children: [ {path: '',component: DefaultTemplateComponent},{path: 'people',component: UnpaidBillListComponent},]
angular 的 select 中 ng-options 设置默认值
angular 的 select 中 ng-options 设置默认值
在AngularJS
开发的后台web系统
中,经常需要对后台数据进行修改,而在修改数据的过程中,若页面中有select
下拉框,需要让其选中原来的数据,即select
中需要设置默认值。
默认值设置方法:
在
select
中使用ng-model
绑定数据myColor
,在对应的
controller
中定义ng-model全局数据$scope.myColor
,并为其赋值,该值即为select
的默认值。
代码如下:
angular.module(''myApp'', [])
.controller(''MyController'', [''$scope''], function($scope) {
$scope.colors = [''black'', ''white'', ''red'', ''blue'', ''yellow''];
$scope.myColor = $scope.colors[2];
})
<div ng-controller="MyController">
<select ng-model="myColor" ng-options="color for color in colors"></select>
</div>
显示效果:
说明: 该文档参考angular官方api
Angular.json - 为测试设置默认配置
如何解决Angular.json - 为测试设置默认配置?
我想知道是否有办法为 ng test
文件中的 angular.json
设置默认配置。
我有这棵树:
...
"projects": {
"myProject": {
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser","options": {
...
},"configurations": {
"production": {
...
},"development": {
...
}
}
},"serve": {
...
"options": {
"browserTarget": "myProject:build:development"
},},"test": {
"options": {
"browserTarget": "myProject:build:development",}
}
}
}
}
所以我通过设置 serve
将 build:development
默认设置为 options.browserTarget
,因此当我运行 ng serve
时,CLI 运行 development
配置。
现在,当我在没有任何 build:development
设置的情况下运行 ng test
时,如果明显没有 --configuration
设置,我应该怎么做才能运行 options.browserTarget
?此配置为 ng test
引发此错误:
Schema validation Failed with the following errors:
Data path "" should NOT have additional properties(browserTarget).
为 ng build
设置默认配置的正确解决方案是什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
angular1.x和angular2+并行,angular1.x 升级 angular2+方案
angular1.x 升级 angular2+ 方案
我给大家提供的是angular1.x和angular5并行,增量式升级方案,这样大家可以循序渐进升级自己的应用,不想看文字直接demo入手migration-from-angular1.x-to-angular2Plus
- 方案1:主体为angular1.x,逐步将angular1.x当中service、component、filter、controller、route、dependencies升级为angular5
- 方案2: 主体为angular5,将项目所有js文件先进行一次加工,采用ES6的方式将每个js文件module
export出来,再逐步将内容向angular5靠近
我建议选择方案1增量式升级,通过在同一个应用中一起运行这两个框架,并且逐个把AngularJS的组件迁移到Angular中。 可以在不必打断其它业务的前提下,升级应用程序,因为这项工作可以多人协作完成,在一段时间内逐渐铺开,下面就方案1展开说明
Hybrid APP主要依赖Angular提供upgrade/static模块。后面你将随处可见它的身影。以下手把手教你将angular1.x迁移到angular2+
1、调用 UpgradeModule 来引导 AngularJS
在AngularJS中,我们会把AngularJS的资源添加到angular.module属性上。 在Angular中,我们会创建一个或多个带有NgModule装饰器的类,这些装饰器用来在元数据中描述Angular资源。在混合式应用中,我们同时运行了两个版本的Angular。 这意味着我们至少需要AngularJS和Angular各提供一个模块。要想引导混合式应用,我们在应用中必须同时引导 Angular 和 AngularJS。要先引导 Angular ,然后再调用 UpgradeModule 来引导 AngularJS。
从HTML中移除ng-app和ng-strict-di指令, 创建一个app.module.ts文件,并添加下列NgModule类:
import { UpgradeModule } from ''@angular/upgrade/static'';
@NgModule({
imports: [
UpgradeModule
]
})
export class AppModule {
constructor(private upgrade: UpgradeModule) { }
ngDoBootstrap() {
this.upgrade.bootstrap(document.body, [''yourAngularJsAppName''], { strictDi: true });
}
}
用AppModule.ngDoBootstrap方法中启动 AngularJS 应用,现在我们就可以使用 platformBrowserDynamic.bootstrapModule 方法来启动 AppModule 了。
main.ts:
import {AppModule} from ''./app/app.module'';
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
platformBrowserDynamic().bootstrapModule(AppModule)
.catch(err => console.log(err));
我们就要开始运行AngularJS+5的混合式应用程序了!所有现存的AngularJS代码会像以前一样正常工作,但是我们现在也同样可以运行Angular代码了
2、将项目中的services逐步升级为angular5
我们将username-service.js里面的内容升级为username-service.ts:
import { Injectable } from ''@angular/core'';
@Injectable()
export class UsernameService {
get() {
return ''nina''
}
}
要在angular1.x中使用UsernameService,先创建一个downgrade-services.ts文件,这里将会存放所有angular5服务降级后在angular1.x中使用的服务
downgrade-services.ts:
import * as angular from ''angular'';
import { downgradeInjectable } from ''@angular/upgrade/static'';
import { UsernameService } from ''./services/ username-service '';
angular.module(''yourAngularJsAppName'')
.factory(''UsernameService'', downgradeInjectable(UsernameService));
完成这两步之后UsernameService就可以在angular1.x controller component service等注入使用了,在angular5中的使用方法这里就不举例了,按照angular5的使用方法来就行
3、项目中的filter逐步升级为angular5的pipe,同时angular1.x的filter依然保留
由于filter的性能问题angular2中已经将filter改为pipe,angular团队没有提供filter升级为pipe,或者pipe降级为filter的module,所以angular1.x中使用filter,angular中使用pipe,filter的升级放在component之前,因为component的template可能会用到
username-pipe.ts:
import { Pipe, PipeTransform } from ''@angular/core'';
Pipe({
name: ''username''
})
export class usernamePipe implements PipeTransform {
transform(value: string): string {
return value === ''nina'' ? ''张三'' : value;
}
}
4、将项目中的component逐步升级为angular5的component
我们将hero-detail.js里面的内容升级为hero-detail.ts:
import { Component, EventEmitter, Input, Output, ViewContainerRef } from ''@angular/core'';
import { UsernameService } from ''../../service/username-service'';
@Component({
selector: ''hero-detail'',
templateUrl: ''./hero-detail.component.html''
})
export class HeroDetailComponent {
Public hero: string;
constructor(private usernameService: UsernameService) {
this.hero = usernameService.get()
}
}
要在angular1.x中使用hero-detail component,先创建一个downgrade-components.ts文件,这里将会存放所有angular5组件降级后在angular1.x中使用的组件
downgrade-components.ts:
import * as angular from ''angular'';
import { downgradeComponent } from ''@angular/upgrade/static'';
import { HeroDetailComponent } from ''./app/components/hero-detail/hero-detail.component'';
angular.module(''yourAngularJsAppName'')
.directive(''heroDetail'', downgradeComponent({ component: HeroDetailComponent }) as angular.IDirectiveFactory)
现在你可以在angular1.x中的template中使用hero-detail组件了,组件之间通讯的问题按照angular5的接口写
5、将angular1.x controller改成angular5 component
现在就剩下controller了,angular2已经取消了controller,controller可以把它当成一个大的component,所以我们按照component的方法重构controller,并且对新的component降级,controller重构之后我们需要修改路由,我们现在使用的还是angular1.x的路由,基本上一个路由对应的是一个controller,这个时候路由可以这样修改:
假设有个TestContentCtrl,对应的路由是test
.state(''test'', {
url: ''/test'',
controller: ''TestContentCtrl'',
controllerAs: ''vm'',
templateUrl: ''./src/controllers/test-content-ctrl.html''
})
在TestContentCtrl改成test-content component后
.state(''test'', {
url: ''/test'',
template: ''<test-content></test-content>''
})
6、第三方插件或者库解决方案
关于项目中引用基于angular1.x的插件或者库,基本都能找到angular2+的版本,可以将angular2+的版本引入进行降级处理就可以在angular1.x中使用了,但是~~~, angular2+的版本很多API都改了,angular1.x中的对应使用方法可能不存在了,这里有两种解决方案
- 引入angular2+的版本,删除angular1.x版本,降级后在angular1.x应用中用到该插件的都检查一次,运用angular2+的版本的API使用该插件
引入angular2+的版本,保留angular1.x版本,angular1.x应用使用angular1.x版本插件,
angular5应用使用angular2+版本插件,方案2增加了项目的体积,相同的插件引用了两个版本。在不影响首屏加载时间的情况下方案2是不错的选择,因为一次性将所有插件或者库的API全部过一遍,工作量比较大容易出错,也不符合我们增量式升级的初衷
现在项目中所有的内容基本都升级为angular5了,我们可以删除downgrade-services.ts和downgrade-components.ts这两个文件了,同时将路由升级为angular5,删除angular1.x相关的库和插件,一个完整的angular5应用就诞生了
如果不是很明白直接看demo:migration-from-angular1.x-to-angular2Plus,喜欢点个赞哟~~~
分享个问题:
如果import angular保错了,你可以考虑引入@types/angular
今天的关于Angular2设置默认路由和angular配置路由的分享已经结束,谢谢您的关注,如果想了解更多关于Angular 2在路由器插座中设置默认内容、angular 的 select 中 ng-options 设置默认值、Angular.json - 为测试设置默认配置、angular1.x和angular2+并行,angular1.x 升级 angular2+方案的相关知识,请在本站进行查询。
本文标签: