我在一个小型Angular2应用程序的主要组件中遇到此错误。这是我目前的代码:
@Component(
selector: 'my-app',
styleUrls: const ['app_component.css'],
templateUrl: 'app_component.html',
directives: const[ROUTER_DIRECTIVES],
providers: const[ClientService, Location])
class AppComponent implements OnInit {
final ClientService _clientService;
Client client;
final Location _location;
AppComponent(this._clientService, this._location);
@override
ngOnInit() async {
var components = _location.path().split('/').skip(1).toList();
if (components.isEmpty) return;
if (components.first=="client" && components.length==2) {
client = await _clientService.getById(components.skip(1).first);
}
}
}
有人知道如何在angular2中获取当前浏览器位置吗?也许使用window.location会更容易。
答案 0 :(得分:2)
将ROUTER_PROVIDERS
添加到bootstrap(...)
并从Location
AppComponent
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
这可能还会解决您使用RouteParams