我花了一刀一個,而做AOT具有角4,始終得到這個錯誤: 未捕獲的ReferenceError:要求沒有定義angular4 AOT擲要求是沒有定義的錯誤
我,因爲我使用jquery得到這個錯誤在我的應用程序,並在應用程序中,我有這樣的代碼:
var jQuery = require('jquery');
jQuery("xxxx").val()
如果我刪除此,整個AOT工作就像一個魅力。
我tsconfig-aot.json:
{
"compilerOptions": {
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"lib": ["es5", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"files": [
"src/app/app.module.ts",
"src/main.ts"
],
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit" : true
}
}
我的彙總配置:
import rollup from 'rollup'
import nodeResolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify'
export default {
entry: 'src/main.js',
dest: 'public/build.js', // output a single application bundle
sourceMap: false,
format: 'iife',
onwarn: function(warning) {
// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if (warning.code === 'THIS_IS_UNDEFINED') { return; }
// intercepts in some rollup versions
if (warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1) { return; }
// console.warn everything else
console.warn(warning.message);
},
plugins: [
nodeResolve({jsnext: true, module: true}),
commonjs({
include: [ 'node_modules/rxjs/**','node_modules/jquery/**']
}),
uglify()
]
}
這是否意味着,如果你使用的需要()你NG4內部應用程序,則AOT不會工作?
感謝並希望聽到您的建議。
我試圖用這個: import *作爲jQuery從'jquery'; 然後它按要求工作,並在JIT模式下,它工作正常。而且,ngc包也很好。但是當我使用捲起來,在出把有此錯誤:
Cannot call a namespace ('jQuery')
src/app/app.component.js (14:8)
12: }
13: AppComponent.prototype.ngOnInit = function() {
14: jQuery('h1').html('New Content');
^
15: };
16: return AppComponent;
這是更新的代碼:
import * as jQuery from 'jquery';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./scss/app.component.scss']
})
export class AppComponent implements OnInit{
ngOnInit(){
jQuery('h1').html('New Content');
}
}
任何想法?
你不應該用'--module es2015'來使用'require',否則會導致TypeScript編譯錯誤。它沒有的唯一原因是你正在使用'var..require'形式而不是正確的,在TypeScript中,'import..require'形式。儘管如此,前面提到的原因仍然不正確。嘗試從'jquery'導入jQuery;如果在運行時拋出jQuery沒有定義,請嘗試從'jquery'導入*作爲jQuery;' –