Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用systemJS在angular2应用程序中包含libphonenumber js?_Javascript_Angular_Systemjs_Libphonenumber - Fatal编程技术网

Javascript 如何使用systemJS在angular2应用程序中包含libphonenumber js?

Javascript 如何使用systemJS在angular2应用程序中包含libphonenumber js?,javascript,angular,systemjs,libphonenumber,Javascript,Angular,Systemjs,Libphonenumber,使用npm安装并更新我的systemjs.config.ts以包括 map:{ ... 'libphonenumber-js': 'node_modules/libphonenumber-js' }, packages: { ... 'libphonenumber-js': { main: './custom.es6.js', defaultExtension: 'js' } }, 并试图使用 import { parse

使用npm安装并更新我的systemjs.config.ts以包括

map:{
    ...
    'libphonenumber-js': 'node_modules/libphonenumber-js'
},
packages: {
    ...
    'libphonenumber-js': {
        main: './custom.es6.js',
        defaultExtension: 'js'
    }
},
并试图使用

import { parse, format, asYouType } from 'libphonenumber-js';
在我的@direction中,我被

找不到模块“libphonenumber js”

我到底该如何将这个库连接到我的应用程序

编辑:
目录布局:
网站名称
websiteName/index.html
网站名称/节点\u模块
websiteName/node_modules/libphonenumber js
网站名称/应用程序
websiteName/app/systemjs.config.ts

Index.html包含:

<script src="/app/systemjs.config.js"></script>
<script>
    System.import('app').catch(function (err) { console.error(err); });
</script>
declare var System: any;
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // other references
            'libphonenumber-js': 'npm:libphonenumber-js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            // other references
            'libphonenumber-js': {
                main: './bundle/libphonenumber-js.min.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

您的包配置不正确。它需要指向
libphonenumber js
的源文件。需要更新引用以匹配项目结构

试试这个:

(功能(全局){
System.config({
路径:{
//路径用作别名
“npm:”:“./node_模块”
},
//地图告诉系统加载器在哪里查找东西
地图:{
//我们的应用程序位于应用程序文件夹中
应用程序:“应用程序”,
//其他参考资料
'libphonenumber js':'npm:libphonenumber js'
},
//包告诉系统加载程序在没有文件名和/或扩展名时如何加载
套餐:{
应用程序:{
main:“./main.js”,
defaultExtension:'js'
},
//其他参考资料
“libphonenumber js”:{
main:'libphonenumber js.min',
defaultExtension:'js'
}
}
});
})(本条);

您确定文件路径正确吗?相对于组件,包从何处提供?@chrispy根据SystemJS中的其他条目似乎是正确的。有没有办法让SystemJS吐出任何路径错误?加载使用系统对象的index.html时,它是无声的。如果SystemJS找不到配置中指定的src文件,则应在控制台中生成
404
。@teddyserne,但是用
node_modules/butt
main:'./bunt.js'
替换数据在typescript传输或页面加载期间仍然不会在任何地方抛出404(并且我已经仔细检查了没有人安装了可能会被拾取的npm臭屁股包)请发布应用程序的文件结构,包括您希望提供的lib文件的位置以及SystemJS配置的位置,无论是单独的文件还是在index.html文件中硬编码的。在/bundle目录中?应该在map对象或packages对象中指定该dir吗?这取决于文件的服务方式。如果您只是直接从
node\u模块
dir中引用文件,则可以使用问题中定义的映射。如果要将多个文件捆绑到一个文件中,则需要将
main
定义更改为指向捆绑文件。不幸的是,我的指令.ts文件仍然带有“找不到模块'libphonenumber js'”的内容,
“../node\u modules'
,应用程序的其余部分也在使用前面的路径。我的错误是,配置与加载上下文相关,即aka
index.html
。请参阅我的最新答案。