我有一個 js 檔案 action.js,我想在 Angular 中使用 javaScript。所以當我讀到它時,它說 js 檔案必須放在資產中,并且路徑必須在 angular.json 的腳本陣列中參考,然后在 ts 和 ngOnInit 中使用宣告,但這會引發一個錯誤,即除此之外該函式未定義禁止訪問js檔案并啟用MIME型別檢查
這是我的js檔案功能
function Di(){
console.log('ffffffffffffff');
}
app.component.ts
import { AstMemoryEfficientTransformer } from '@angular/compiler';
import { Component, OnInit } from '@angular/core';
declare function Di(): any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit{
title = 'Calculator';
ngOnInit() {
Di();
}
}
角.json
"styles": [
"src/styles.scss"
],
"scripts": ["src/assets/js/action.js"]
},
app.component.html
</div>
</div>
<script src = "/src/assets/action.js"></script>
</body>
提前致謝
想在 Angular 中使用 js 函式
uj5u.com熱心網友回復:
嘗試創建一個 di.ts 檔案并在其中匯出您的函式
export default function Di(){}
然后將其匯入您的AppComponent
import Di from 'di'
uj5u.com熱心網友回復:
創建一個 ts 檔案而不是 js,然后像這樣創建你的函式
匯出函式 Di() { console.log('Di') }
然后將其匯入您的組件。
如果這對你有用,我希望你通知我,請。
uj5u.com熱心網友回復:
不確定我是否理解這個功能的意義——它應該通過點擊來執行嗎?還是使用組件創建?假設從 html 模板單擊按鈕,您應該將此 fn 作為公共欄位添加到組件中,并使用 html 中的括號呼叫它
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532587.html
