2017-02-09 417 views
0

我以下的角2當然其中講師導航到他想要生成的成分的特定路徑,然後運行下面的命令:如何在angular-cli中將組件生成路徑更改爲當前路徑?

ng generate component component-name 

他運行它後,將工具生成裏面的文件當前目錄,但是當我嘗試相同時,該工具最終會在應用程序根文件夾中生成文件。

換句話說,如果我這樣做:

app\my-component> ng generate component component-name 

我期待命令在這裏生成文件:

app\my-component\component-name\ 
    component-name.component.html 
    component-name.component.ts 
    ... 

但取而代之的是在這裏做它:

app\component-name\ 
    component-name.component.html 
    component-name.component.ts 
    ... 

我如何告訴ng-cli使用當前路徑?

回答

5
ng g c 'path/to/component/componentName' 

使用角度cli時,您不應該離開根目錄。如果你想所有組件進入一個子目錄叫我的分量,你會只是做ng g c my-component/componentName

注意,G和C分別是generatecomponent有效的精簡版。

+0

感謝您的答覆。但我正在問這個問題,正是因爲課程導師像這樣工作。我有一個同事也使用它......只是導航到您想要生成組件的路徑。 – jacoviza

+1

角度cli正在不斷髮展。這可能是一個已被棄用的功能。根據他們的GItHub文檔,當前的建議是按照我所演示的路徑名稱傳遞。 –

+0

是的,我明白現在正在積極開發中,所以我期望看到一些這樣的不一致之處......可能已被棄用......但有趣的是,我的同事有一個稍微大一點的角-cli版本。他正在使用1.0.0-beta.26。我正在使用1.0.0-beta.25.5 – jacoviza

0

當跟隨Max Schwartzmuller的Udemy課程時,我遇到了類似的情況。我試圖在與應用程序平行的子文件夾中生成組件。 cli將創建任何直接在app下面調用的方式。

c:\ng2\aufbs\src\mycomponents>ng g c my-test 
installing component 
    create src\app\my-test\my-test.component.css 
    create src\app\my-test\my-test.component.html 
    create src\app\my-test\my-test.component.spec.ts 
    create src\app\my-test\my-test.component.ts 

移動/ mycomponents下的應用程序一切都按預期工作。

0

設置應用程序文件夾的路徑,所以你不需要使用ng g c 'C:\someFolder\your-project\src\app\some-folder\other-folder\your-component'。 只是用ng g c '.\pages\shered\reset-password'

0

假設你有結構src/app,你想創建住在src/app/components/組件header可以運行ng g c components/header,這將在src/app/components/header文件夾中生成的文件,如src/app/components/header/header.component.tssrc/app/components/header/header.component.html