簡體   English   中英

將變量從 index.html 發送到 app.component.ts

[英]sending variable from index.html to app.component.ts

我可以將變量從 index.html 發送到 app.component.ts 我在 index.html 中有這個腳本,我想在刪除查詢參數到 app.component.ts 之前發送可變的 url

  <script type="text/javascript">
        var url = window.location.toString();
       if(url.indexOf("?") > 0) {
          var sanitizedUrl = url.substring(0, url.indexOf("?"));
          window.history.replaceState({}, document.title, sanitizedUrl);
        }
      </script>

當然可以。 我做了一個小演示 ,向您展示了如何。

  1. 您所需要做的就是像在index.html中一樣包裝代碼。
  2. 您必須聲明變量。 您可以在組件內部執行以下操作
import { Component } from '@angular/core';

// notice that foo is inited in index.html
declare var foo;

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})

typings.d.ts 我建議在types.d.ts中聲明它。 如果您使用的是angular-cli,我認為它位於src/app/typings.d.ts

declare var foo: any;

就是這樣。 您可以在應用程序中的任何位置使用變量。

在 .html 文件中 -->

<script>
   const baseUrl = "http://0.0.0.0/";
</script>

在 .ts 文件中聲明變量並使用它 -->

declare var baseUrl;
export const BASE_URL = baseUrl;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM