【问题标题】:How can use external js files into angular.json in angular 6如何在 Angular 6 中使用外部 js 文件到 angular.json
【发布时间】:2018-09-17 06:14:57
【问题描述】:

我有一个侧边栏菜单脚本文件,位于我包含在index.html 中的资产文件夹中。每当我刷新 home/dashboard 子路由时,脚本都会运行,如果我从初始路由 / 重定向到 home/dashboard,侧边栏显示/隐藏按钮不起作用(此逻辑是脚本的一部分,我提到)一旦我刷新页面,外部脚本将正常工作。这是我的路由模块

 app.routing.module.ts

 const routes: Routes = [
      { path: '', redirectTo: '/home', pathMatch: 'full' },
      //{ path: 'login', component: LoginComponent,  },
      { path: 'home', component: HomeComponent,
      children:[
        { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
        { path: 'dashboard', component: DashboardComponent,},
        { path: 'totalapproved', component: ApprovedComponent,},
        { path: 'campaign', component: CampaignmanagerComponent,},]}
    ]

有没有办法将外部资产脚本添加到 angular.json 文件中?

【问题讨论】:

  • 分享更多细节并正确陈述您的问题。无法准确理解您想要什么。
  • 准确理解您的问题有点困难,但我认为您的问题是我们的外部脚本只会在初始页面加载时执行。因此,如果您刷新页面,然后导航到子路由,那么它们将不起作用。这是正常行为

标签: angular angular2-routing angular6 angular-router app-route


【解决方案1】:

您可以在 angular.json 文件中添加外部脚本和样式,例如

"styles":[ 
"src/assets/css/style.css"
],
"scripts"[
"src/assests/js/script.js"
]

【讨论】:

    【解决方案2】:
    "styles":[ 
    "/style.css"
    ],
    
    "scripts"[
    "src/script.js"
    ]
    

    如果您使用 CDN,也可以将其添加到您的 index.html。

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-18
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多