00003.將組件加載到html容器中

將組件加載到html容器中
三大框架都是spa,在html文件中寫出 root的<div>對象(通常叫做app或者root),然后將根組件渲染給root對象

比如html內(nèi)容:通過npm腳手架生成的項目,html的文件名字應(yīng)該如下

  • Angular:src/index.html
  • Vue:index.html
  • React: public/index.html
  <div id="root"></div> <!-- 容器元素 -->

Angular

在Angular中,通過模塊文件注冊組件,根模塊文件通常是app.module.ts,你也可以自定義自己的模塊文件xxx.module.ts。
在模塊文件中導(dǎo)入組件或其他模塊,然后在@NgModule裝飾器的declarations數(shù)組中注冊組件,在import數(shù)組中注冊其他模塊:

將組件和路由注冊到模塊中:app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AppRoutingModule } from './/app-routing.module';
import { YourComponentNameComponent } from './your-component-name/your-component-name.component';
 
@NgModule({
  declarations: [
    AppComponent,
    YourComponentNameComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
加載根模塊:main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));
根據(jù)路由顯示當前路徑對應(yīng)文件:src\app\app-routing.module.ts
const routes: Routes = [
  { path: '', redirectTo: '/home', pathMatch: 'full' },
  { path: 'home', component: HomeCustomComponent, canActivate: [AwagAuthVerifyToken] },
];

入口文件是在angular.json文件中指定的,這里不再繼續(xù)討論,實際開發(fā)中通常也是無需關(guān)注的。

2.Vue:

加載根組件和路由src/main.js

import Vue from 'vue';
import App from './App';
import router from './router';
import store from './store';

new Vue({
  router,
  store,
  render: h => h(App),
}).$mount('#root');
根據(jù)路由顯示當前路徑對應(yīng)文件:src\app\app-routing.module.ts
import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

export const constantRoutes = [
  {
    path: '',
    redirect: '/EnglishChinese',
  },
  {
    path: '/EnglishChinese',
    name: 'EnglishChinese',
    // component: Menu
    component: () => import('@/views/english_chinese/Main'),
    children: [
      {
        path: 'WordList',
        name: 'WordList',
        components: {
          default: () => import('../views/english_chinese/words/WordList'),
        },
      },
      {
        path: 'CountChart',
        name: 'CountChart',
        components: {
          default: () => import('../views/english_chinese/count/CountChart'),
        },
      },
    ],
  },
];
const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
  mode: 'history',
});
const router = createRouter();

export default router;

3.React:src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from '.App';
import reportWebVistals from './reportWebVitals';
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容