如何在Angular 2/Angular中解析tsx文件-Cli [英] How can I resolve tsx file in Angular 2 / Angular - Cli

查看:247
本文介绍了如何在Angular 2/Angular中解析tsx文件-Cli的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.tsx文件,并通过以下方式将其导入ts文件中

I have a .tsx file and import it in a ts file via

import {ReactChildComponentView} from "./MessageBox";

但是当我运行ng服务时会引发错误

but when I run ng serve it throws an error

    ERROR in ./src/app/react/wrapper.component.ts
    Module not found: Error: Can't resolve './MessageBox' in '.../src/app/react'

在Chrome控制台中,我可以看到以下错误

In Chrome console I can see the following Error

using description file: .../package.json (relative path: ./src/app/react/MessageBox)
  as directory
    .../src/app/react/MessageBox doesn't exist
  no extension
    .../src/app/react/MessageBox doesn't exist
  .ts
    .../src/app/react/MessageBox.ts doesn't exist
  .js
    .../src/app/react/MessageBox.js doesn't exist

似乎.angle没有寻找.tsx文件.我该如何更改?

it seems like angular doesn't look for .tsx files. How can I change that?

推荐答案

首先,您需要确保已配置 webpack 来查找扩展名为 .tsx 的文件在您的Webpack配置中的resolve.extensions中添加.tsx:

First you need to make sure you have configured webpack to look for files with .tsx extension by adding .tsx in resolve.extensions in your webpack config:

resolve: {
    extensions: [
        '.js',
        '.ts',
        '.tsx'
    ]
}

然后,您需要通过在Webpack配置的module.loaders中为ts加载器设置test: /\.tsx$/来配置Typescript加载器以查找.tsx文件:

Then you need to have typescript loader configured to look for .tsx files by setting test: /\.tsx$/ for ts loader in module.loaders in webpack config:

module: {
    loaders: [
      {
        test: /\.tsx$/,
        exclude: /node_modules|typings/,
        loaders: [
          'ts'
        ]
      },
    // More loaders
   ]
}

此外,您还可以在 Typescript手册

这篇关于如何在Angular 2/Angular中解析tsx文件-Cli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆