我是Angular2的新手.我的项目的技术堆栈是Angular2,以typescript和spring为后端.我不想按照指示使用node server来编译前端,但我需要使用TOMCAT和Maven.我有几个问题.

  1. 我猜 node 服务器会生成.js和.js.每一张 map .ts文件,因为浏览器只能理解.js文件.我的理解正确吗?如何使用Maven和Tomcat完成这项任务?
  2. 我想使用Maven从头开始构建我的应用程序.我更喜欢鲍尔担任前端任务经理.

有谁能给我一个使用"bower或任何其他前端任务管理工具,如缩小文件、创建应用程序支架"和"Maven for backend task management"创建Angular2+Spring应用程序的分步指南吗?我愿意接受任何建议.

推荐答案

我用的是打字脚本.ts文件在我的Angular 2+Spring bootstrap 应用程序和maven中.我对依赖项运行npm install,对转换运行npm run tsc.ts文件到.js乘exec-maven-plugin.

下面是我的pom中的插件部分.xml.在我的应用程序中,pacake.json,tsconfig.json和打字.json都在src/main/resources路径下,所以在该路径下运行npm任务

pom.xml

<parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.3.5.RELEASE</version>
</parent>

<packaging>war</packaging>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>exec-npm-install</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <workingDirectory>${project.basedir}/src/main/resources</workingDirectory>
                        <executable>npm</executable>
                        <arguments>
                            <argument>install</argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
                <execution>
                    <id>exec-npm-run-tsc</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <workingDirectory>${project.basedir}/src/main/resources</workingDirectory>
                        <executable>npm</executable>
                        <arguments>
                            <argument>run</argument>
                            <argument>tsc</argument>
                        </arguments>
                    </configuration>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

我的Angular2+Spring Boot应用程序文件夹 struct 如下所示

src/main/resources
                  /app          - .ts and converted .js
                  /css
                  /images
                  /js           - systemjs.config.js is also placed here
                  /node_modules - generated by npm install and will include in war
                  /typings
                  application.properties
                  package.json
                  tsconfig.json
                  typings.json

src/main/webapp
               /WEB-INF
                       /jsp     - all .jsp files

在…上jsp文件头部分,包括systemjs.config.js

<script type="text/javascript" src="webjars/zone.js/0.6.12/dist/zone.js"></script>
<script type="text/javascript" src="webjars/reflect-metadata/0.1.3/Reflect.js"></script>
<script type="text/javascript" src="webjars/systemjs/0.19.27/dist/system.js"></script>
<script type="text/javascript" src="js/systemjs.config.js"></script>

这里还有我的WebMVCConfigureAdapter代码,用于映射路径

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.my.controller")
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/images/**")) {
            registry.addResourceHandler("/images/**").addResourceLocations("classpath:/images/");
        }
        if (!registry.hasMappingForPattern("/css/**")) {
            registry.addResourceHandler("/css/**").addResourceLocations("classpath:/css/");
        }
        if (!registry.hasMappingForPattern("/js/**")) {
            registry.addResourceHandler("/js/**").addResourceLocations("classpath:/js/");
        }
        if (!registry.hasMappingForPattern("/app/**")) {
            registry.addResourceHandler("/app/**").addResourceLocations("classpath:/app/");
        }
        if (!registry.hasMappingForPattern("/node_modules/**")) {
            registry.addResourceHandler("/node_modules/**").addResourceLocations("classpath:/node_modules/");
        }
    }

    @Bean
    public InternalResourceViewResolver internalViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

我想提到的一件事是,如果操作系统是Windows或Mac,在eclipse上运行exec maven插件时会遇到一些黑客攻击.Linux(Ubuntu)看起来一点问题都没有.

为了解决这个问题,我做了一些调整.

lrwxr-xr-x     1 root   wheel        17 May 22 03:01 node -> ../local/bin/node
lrwxr-xr-x     1 root   wheel        44 May 22 02:50 npm -> ../local/lib/node_modules/npm/bin/npm-cli.js

对于Windows,我创建了node.bat和npm.系统路径下的bat文件如下

npm.bat

@echo off
set arg1=%1
set arg2=%2
set arg3=%3
set arg4=%4
C:\Progra~1\nodejs\npm.cmd %arg1% %arg2% %arg3% %arg4%

Typescript相关问答推荐

返回签名与其他类型正确的函数不同的函数

如何修复VueJS中的val类型不能赋给函数

Tailwind CSS样式不在Svelte应用程序中呈现

替代语法/逻辑以避免TS变量被分配之前使用." "

如何创建泛型类型组件来使用React Native的FlatList或SectionList?'

数组文字中对象的从键开始枚举

对扩展某种类型的属性子集进行操作的函数

向NextAuth会话添加除名称、图像和ID之外的更多详细信息

对于合并对象中的可选属性(例如选项),类型推断可能是错误的

保护函数调用,以便深度嵌套的对象具有必须与同级属性函数cargument的类型匹配的键

APP_INITIALIZER在继续到其他Provider 之前未解析promise

转换器不需要的类型交集

为什么我在这个重载函数中需要`as any`?

ANGLE NumberValueAccessor表单控件值更改订阅两次

打字脚本中的模块化或命名空间

扩展对象的此内容(&Q;)

打字脚本错误:`不扩展[Type]`,而不是直接使用`[Type]`

导航链接不触发自定义挂钩

显式推断对象为只读

具有枚举泛型类型的 Typescript 对象数组