react路由的基本用法(一)

2021-10-10 22:46:51 字數 1436 閱讀 1393

一、 在你的react專案中安裝react-router,我這裡指定了版本號3.2.0,安裝命令如下:

npm i [email protected]
二、 在專案的scr目錄下建立乙個資料夾,我這裡取名為reactrouter,名字不是規定的,可以隨便命名。然後建立檔案如下:
import react from

'react'

;export

default

class

extends

react.component

}render()

}

index.jsx

import react from

'react'

;export

default

class

index

extends

react.component

render()

}

list.jsx

import react from

'react'

;export

default

class

list

extends

react.component

render()

}

三、 專案的src目錄的入口檔案index.js中,**如下

index.js

import react from

'react'

;import reactdom from

'react-dom'

;//引入路由

import

from

'react-router'

;//引入對應的元件

;import index from

'./reactrouter/index'

;import list from

'./reactrouter/list'

;let routes =

>

"/" component=

/>

"/index" component=

/>

"/list" component=

/>

<

/router>

reactdom.

render

( routes,

document.

getelementbyid

('root'))

;

這樣乙個簡單的路由就實現了,效果如下:

React 基本用法

元素 用 classname 來代替 class htmlfor 替換 lable 的 for 屬性 標籤 標籤必須成對出現,如果是單標籤,則必須自閉和 注釋推薦使用 也可也用 這是注釋 這是注釋 這是注釋當編譯引擎,在編譯 jsx 的時候,如果遇到了 那麼會把它當作 html 去編譯,如果遇到了 ...

React中路由的基本使用

1 react路由介紹 現在我們來搞一搞react中的路由吧,別問我為什麼這木喜歡用搞這個字,因為它比較深奧。注意下面我們使用的是react router dom react中的路由基本使用還是滿簡單的,零碎的小東西有點多,所以我直接把他們揉到一起做了乙個小例子,我都寫上注釋了,應該挺簡單易懂的 注...

vue路由的基本用法

doctype html en utf 8 路由基本用法 title router link active active style js vue.js script js vue router.js script head itany 使用router link元件來定義導航,to屬性指定鏈結ur...