typescript部分
export關鍵字作用:可以讓定義的類型在全局范圍內能被訪問到。
this關鍵字的指向:
1,一般情況下,this指向的是本對象,即用“{}”括起來的內容(函數(shù)的“{}”除外)。
2,對于函數(shù)嵌套情況,外層函數(shù)就指向本對象,而內層函數(shù)中的this可能指向其調用者,甚至不確定調用者,
因此若內層函數(shù)想使用外層函數(shù)中的變量,直接使用,無需加this,若要使用對象中的變量,則需要使用箭頭函數(shù)“()=>”。any可用于聲明某個不確定數(shù)據(jù)類型的變量。
加“?”的變量在初始化的不用賦初值
interface Shape {
name: any;
width: number;
height: number;
color?: string;
}
function area(shape : Shape) {
var area = shape.width * shape.height;
return "I'm " + shape.name + " with area " + area + " cm squared";
}
console.log( area( {name: "rectangle", width: 30, height: 15} ) );
console.log( area( {name: "square", width: 30, height: 30, color: "blue"} ) );
- interface和class的區(qū)別(補充):
1,因為typescript是JavaScript的超級,為了保證兼容,interface就完全變成了js,沒有類的痕跡,而class編譯后還會保留有class的標記
2,通常interface只聲明變量,不包括方法(這一點目前不是很確定)
angular部分
-
對于angular三大核心的理解:
1, 組件(component)。 angular的頁面全由各部分組件(component)構成,組件可能包括.scss,.html,.ts文件。即組件并不是某一種卻定的文件類型,而是
實現(xiàn)某一個頁面的各種類型的文件集合。組件即為頁面服務。2,模塊(module)。菜鳥教程看的有點迷,等待老師詳講。
3,路由(router)。主要用于前端確定瀏覽器輸入的地址由哪個組件(component)處理。
angular框架流程:
- 瀏覽器地址 -> 路由(router)-> 組件(component)-> 服務(service)
- 其中組件中的.html文件我們通常稱之為模板,用于確定要顯示的內容;對于上述流程中,還會有一個derictive指令,用于控制模板內容的顯示方式
瀏覽器顯示內容過程:
index.html -> <app-root></app-root>進行頁面內容替換 -> 默認進入app.component.html頁面-> <router-outlet></router-outlet>路由插槽 -> 插入子頁面內容。
該過程中自始至終都只是替換了index.html文件中的內容。
前端頁面獲取并顯示數(shù)據(jù)具體工作流程:
定義model類->寫對應的service文件->組件中的component.ts文件->component.html文件
model類:與后端的model相對應,或與數(shù)據(jù)庫的表相對應,因為只包含成員變量,因此通常用interface定義,如下:
export interface Test {
name: string;
body: string;
}
service:與后端的controller相對應,負責向后端請求數(shù)據(jù),以及向組件傳遞后端返回的數(shù)據(jù)。
component.ts:獲取service傳回來的數(shù)據(jù),并對它進行簡單的處理,然后賦值給自己的成員變量,便于component.html模板文件獲取要展示的內容。
component.html:組件中的該部分內容用于頁面展示,可用“{{}}”獲取component.ts傳過來的對象。