标签: angular8

如何在包含 RouterOutlet 的 HTML 模板中直接使用激活路由的数据?

这个我花了一段时间才弄明白,所以我希望通过在 SO 的问答方式中分享解决方案来节省某人的时间。它是这样的:

目标

我有一个 Angular8 Web 应用程序,我使用RouterModule在组件之间导航。

以下是我的路线的定义方式:

const routes: Routes = [
    { path: 'moo', component: AppMooComponent, data: { title: 'Moo Section', desc: 'Moo is the first example component.' } },
    { path: 'boo', component: AppBooComponent, data: { title: 'Boo Section', desc: 'Boo is the second example component.' } },
    { path: 'foo', component: AppFooComponent, data: { title: 'Foo Section', desc: 'Foo is the third example component.' } },
    { path: '', redirectTo: 'moo', pathMatch: 'full' …
Run Code Online (Sandbox Code Playgroud)

angular2-routing angular angular8

7
推荐指数
1
解决办法
1497
查看次数

vscode ng 服务一次又一次编译

在 Windows 10 上,ngserve 会一次又一次地进行编译,而不保存任何更改。即使当我在 vscode 中的两个文件之间切换时,它也会重新编译。

但是当我在 macOS 上运行相同的 Angular 项目时,它的行为正常。

npm angular-cli angular angular8 angular-cli-v8

6
推荐指数
0
解决办法
2256
查看次数

使用自定义 ngDoBootstrap 和 createCustomElement 时,Angular Router 会忽略 URL

我使用自定义ngDoBootstrap函数而不是默认函数时bootstrap: [AppComponent],如下所示:

@NgModule({
  imports:      [ BrowserModule, FormsModule, AppRoutingModule ],
  declarations: [ AppComponent, HelloComponent ],
  exports: [ AppComponent ],
  entryComponents: [ AppComponent ],
  // bootstrap: [AppComponent],
})
export class AppModule {
  constructor(private injector: Injector) {
  }

  public ngDoBootstrap(): any {
    const appElement = createCustomElement(AppComponent, { injector: this.injector });
    customElements.define('my-app', appElement);
  }
}
Run Code Online (Sandbox Code Playgroud)

然后 应用程序路由被破坏。

它会忽略 URL 中的任何更改,并且仅在我单击 时才起作用 <a [routerLink]='...'>。此外,初始路线 / 未加载。

它一定是由自定义引导机制引起的,因为当我取消注释时bootstrap: [AppComponent],一切正常。

完整代码可在此处获取:stackblitz 示例(由于 stackblitz 使用的 typescript 版本,需要下载并在本地运行)

如何使路由与自定义应用程序模块引导一起工作?

typescript custom-element angular angular-router angular8

6
推荐指数
1
解决办法
1409
查看次数

发生未处理的异常:工作区中未设置配置“生产”

您好 在 Angular 8 中创建了一个项目。最初它只支持一种默认语言(US-EN)。然后我应用了本地化。

在准备生产构建的本地化之前,我曾经给出以下命令。

ng build --prod --base-href "/Windchill/com/qiwkCollaborator/"
Run Code Online (Sandbox Code Playgroud)

本地化后,我在 angular.json 和 package.json 中做了一些更改。在进行这些更改后,每当我发出任何准备构建它的命令时,都会出现以下错误。

An unhandled exception occurred: Configuration 'production' is not set in the workspace.
Run Code Online (Sandbox Code Playgroud)

Package.json 文件的某些部分如下:

"name": "qwik-collaborator",
  "version": "1.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build-locale:en": "ng build --prod --i18n-locale en --i18n-format xlf --i18n-file src/translate/messages.en.xlf --output-path=dist/en --baseHref /en/",
    "build-locale:fr": "ng build --prod --i18n-locale fr --i18n-format xlf --i18n-file src/translate/messages.fr.xlf --output-path=dist/fr --baseHref /fr/"
Run Code Online (Sandbox Code Playgroud)

Angular.json …

dev-to-production angular-cli angular angular8

6
推荐指数
1
解决办法
8834
查看次数

类型“void”不可分配给类型“ObservableInput&lt;any&gt;”

我收到以下错误

类型“(user: User) => void”的参数不可分配给类型“(value: User, index: number) => ObservableInput”的参数。类型“void”不可分配给类型“ObservableInput”。

 getCurrentUserDb()
  {
    return this.login.authState.pipe(
                        switchMap(user=>{
                          return this.serviceUsers.getUserByuid(user.uid);
                        }),
                        map(user=>{
                          return user;
                        })
    )
  }
Run Code Online (Sandbox Code Playgroud)

rxjs angular angular-observable angular8 angular9

6
推荐指数
1
解决办法
1万
查看次数

@ngx-translate/core 中的错误 - 错误 TS1086:无法在 Angular 8 的环境上下文中声明访问器

我正在使用 Angular 8 并使用 ngx-translate 实现国际化 (i18n)。我使用了以下库。

  1. "@ngx-translate/core": "^13.0.0",
  2. “@ngx-translate/http-loader”:“^6.0.0”。

在运行这个 angular 项目时,我遇到了那个错误。

请帮我解决这个错误。

这是完整的错误:

ERROR in node_modules/@ngx-translate/core/lib/translate.directive.d.ts:14:9 - error TS1086: An accessor cannot be declared in an ambient context.

    node_modules/@ngx-translate/core/lib/translate.directive.d.ts:15:9 - error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@ngx-translate/core/lib/translate.service.d.ts:49:9 - error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@ngx-translate/core/lib/translate.service.d.ts:56:9 - error TS1086: An accessor cannot be declared in an ambient context.
    node_modules/@ngx-translate/core/lib/translate.service.d.ts:63:9 - error TS1086: An accessor cannot be declared in an …
Run Code Online (Sandbox Code Playgroud)

angular angular8

6
推荐指数
1
解决办法
4597
查看次数

如何等待订阅功能以角度 8 完成?

我想等待一个函数完成,然后再执行它旁边的函数。我有一个名为getData() 的函数,其中发生了 http 调用,它返回一个 observable。第二个函数checkDuplicate()我们订阅了该函数getData()。我们有第三个函数,称为proceed(),我们在其中调用checkDuplicate()函数,一旦checkDuplicate()函数完成,我们就会调用alert("finished")。但这里的问题是在checkDuplicate()函数完成之前,警报已经被触发。

找到代码以获得更好的说明:

getData(): Observable<any>{
  return this.http.get<any>(URL...);
}

checkDuplicate(){

return this.getData().subscribe(response => {
   if(response){
      console.log("Inside");
  }
});
}

proceed(){
 this.checkDuplicate();
 console.log("finished");
}
Run Code Online (Sandbox Code Playgroud)

实际结果

完成的

里面

预期结果

里面

完成的

我试过asyn/await等待 checkDuplicate() 函数完成。但是还是没有用。如果您分享解决方案,将不胜感激。提前致谢 !!!

wait subscribe observable async-await angular8

6
推荐指数
1
解决办法
1万
查看次数

ReferenceError: google 未定义 Angular Universal SSR npm run server:ssr

我知道 google 引用错误出现的原因是因为在服务器上执行的 sever.js 没有在其范围内定义 google 。我正在寻找解决方案来修复此错误。

运行后npm run serve:ssr得到这个

task: ZoneTask {
    _zone: Zone {
      _parent: [Zone],
      _name: 'angular',
      _properties: [Object],
      _zoneDelegate: [ZoneDelegate]
    },
    runCount: 0,
    _zoneDelegates: null,
    _state: 'notScheduled',
    type: 'microTask',
    source: 'Promise.then',
    data: ZoneAwarePromise [Promise] {
      __zone_symbol__state: 0,
      __zone_symbol__value: ReferenceError: google is not defined
          at new exports.ListingsComponent (/home/abhishek/projects/website/dist/server.js:50760:350264)
          at createClass (/home/abhishek/projects/website/dist/server.js:79119:20)
          at createDirectiveInstance (/home/abhishek/projects/website/dist/server.js:78988:20)
          at createViewNodes (/home/abhishek/projects/website/dist/server.js:87348:36)
          at createRootView (/home/abhishek/projects/website/dist/server.js:87262:5)
          at Object.createProdRootView [as createRootView] (/home/abhishek/projects/website/dist/server.js:87774:12)
          at ComponentFactory_.create (/home/abhishek/projects/website/dist/server.js:78467:29)
          at RouterOutlet.activateWith (/home/abhishek/projects/website/dist/server.js:183250:40)
    },
    scheduleFn: undefined,
    cancelFn: …
Run Code Online (Sandbox Code Playgroud)

google-maps node.js server-side-rendering angular-universal angular8

6
推荐指数
0
解决办法
321
查看次数

如何检查响应式表单中的表单控件是否被禁用

有没有一种方法可以检查表单控件是否被禁用?

this.form.get("nationality").disable()
Run Code Online (Sandbox Code Playgroud)

禁用此控件。

我可以检查此控件是否在重置方法中禁用。如果禁用,则不需要重置该值。

我该如何检查。

onReset(){
 if(this.form.get("nationality").isDisable()){// its wrong
let name =this.form.get("nationality").value;
}else{
let name = null;
}
 this.form.reset({
        name: name
    });

}
Run Code Online (Sandbox Code Playgroud)

typescript angular8

6
推荐指数
1
解决办法
9918
查看次数

Angular 9 无法使用 AWS S3 和 Cloudfront 加载模块脚本

我使用 S3 和 CloudFront 服务在 AWS 中托管我的 Angular 9 应用程序。访问应用程序后,我在浏览器中收到以下错误:

\n
    \n
  • 无法加载模块脚本:服务器响应非 JavaScript MIME 类型“text/html”。根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。\n/polyfills-es2015.5af60e74e954ace452ee.js:1
  • \n
  • 无法加载模块脚本:服务器响应非 JavaScript MIME 类型“text/html”。根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。\n/main-es2015.9d477ae8338087e639b5.js:1
  • \n
  • 无法加载模块脚本:服务器响应非 JavaScript MIME 类型“text/html”。根据 HTML 规范对模块脚本强制执行严格的 MIME 类型检查。
  • \n
\n

我的index.html:

\n
<!doctype html>\n<html>\n<head>    \n  <meta charset="utf-8">\n  <title>ReachApp Success</title>\n  <base href="/reach">\n    <!-- TODo: check for common configuration for material icon -->\n    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> \n  <meta name="viewport" content="width=device-width, initial-scale=1">\n  <link rel="icon" type="image/x-icon" href="favicon.ico">\n  <script src='https://www.google.com/recaptcha/api.js' async\xc2\xa0defer></script> \n<link rel="stylesheet" href="styles.7f06624db32e037ad1c6.css"></head>\n<body>\n  <reach-root></reach-root>\n …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 amazon-cloudfront angular angular8 angular9

6
推荐指数
1
解决办法
4218
查看次数