美文网首页
angular起步

angular起步

作者: 渣渣曦 | 来源:发表于2019-06-17 09:10 被阅读0次

1、安装

首先在https://nodejs.org网站上下载Node.js

安装完成Node.js后,接着运行以下命令安装Typescript:

npm install -g typescript  // -g 参数代表 typescript 安装到node.js的全局目录中

运行以下命令安装angular-cli (该工具使用命令行创建和管理项目):

npm install -g @angular/cli@latest

2、示例项目

2.1安装运行

运行以下命令创建示例项目:

ng new angular4Example

执行完成后显示下面信息:

image

切换到angular4Example目录下:

cd angular4Example

执行npm install安装项目依赖模块:

npm install

执行以下命令运行示例程序:

ng serve --open

2.2生成Component

执行以下命令生成helloworld组件:

ng generate component helloworld

生成组件后删除app/app.component.html文件内容,然后写入helloworld组件标签:

<app-helloworld></app-helloworld>

页面显示如下:

image

修改helloworld.component.ts代码如下:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-helloworld',
  templateUrl: './helloworld.component.html',
  styleUrls: ['./helloworld.component.css']
})
export class HelloworldComponent implements OnInit {
  name: string; // <---增加name属性
  constructor() {
    this.name = '世界';  // <---为name变量赋值
  }

  ngOnInit() {
  }

}

修改helloworld.component.html内容为:

<p>
  hello {{name}}
</p>

显示页面如下:

image

通过构造隐函数进行赋值,通过{{}}在模板进行显示

3、一些使用方面的技巧

3.1 跨域访问
需在chrome浏览器图标右键->属性,在目标框里输入以下代码:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --user-data-dir="C:/Chrome dev session" --disable-web-security

3.2 开发环境配置
需要在environment目录下配置environment.prod.ts(生产环境)和environment.ts(开发环境)代码

export const environment = {
  production: false,
  baseUrl: 'http://localhost:80',
  websocketURL: 'ws://localhost:80'
};

3.3 打包路径设置
把index脚本引入放在"scripts"中:在angular.json中"projects"->"architect"->"build"->"configurations"->"production"

 "deployUrl": "/scripts/",

相关文章

  • angular起步

    1、安装 首先在https://nodejs.org网站上下载Node.js 安装完成Node.js后,接着运行以...

  • Angular起步篇

    1:环境搭建 今天给大家介绍4种环境搭建的方法。 一:Angular-cli的安装 官方指导文档:www.angu...

  • Angular 8 起步教程

    这就是这个示例教程的最终效果。 上面一个导航条,然后是2个页面。 创建项目 创建完成后,运行: 创建导航 打开 /...

  • 使用Angular4 CLI + Electron 编写桌面应用

    起步—— 开发前的准备工作 (注意: 此时 @angular/cli 版本为 1.4.2, electron 版本...

  • Angular2 项目起步

    0.创建配置文件 使用DOS命令切换到目标目录下,运行命令 创建并转到目录中. 1.手动编写配置文件 新建文件 p...

  • angular6.X--起步

    一、安装最新版本的nodejs 注意:请先在终端/控制台窗口中运行命令 node -v 和 npm -v, 来验证...

  • 【1】Angular(简称ng)起步

    一、Angular 是什么? Angular(读音['æŋgjʊlə])是一套用于构建用户界面的 JavaScri...

  • angular使用iosSelect插件实现城市选择

    因为使用angular写一个webapp页面,里面有城市选择,为了简单一点直接使用了IOSSelect插件. 起步...

  • angular文档——CLI-快速起步

    CLI 快速起步 使用 CLI 工具快速构建 Angular 应用。 相比手动的做一切事情,好的工具使应用开发更快...

  • [Angular学习笔记]起步和环境搭建

    一、本地开发环境搭建 1.node环境 根据官方文档说明,angular需要Node.js 版本 10.9.0 或...

网友评论

      本文标题:angular起步

      本文链接:https://www.haomeiwen.com/subject/tqdofctx.html