欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue轉(zhuǎn)react入門指南

 更新時(shí)間:2021年10月28日 15:56:34   作者:唱跳rap和web  
因?yàn)樾鹿臼褂胷eact技術(shù)棧,包括Umi、Dva、Ant-design等一系列解決方案。本文就簡(jiǎn)單的介紹一下vue轉(zhuǎn)react入門指南,感興趣的可以了解一下

因?yàn)樾鹿臼褂胷eact技術(shù)棧,包括Umi、Dva、Ant-design等一系列解決方案。稍微熟悉一下之后,了解到雖然有些不同,但是還是大相徑庭。以下我就以兩個(gè)火熱的框架react16&vue2(在積極學(xué)習(xí)vue3中)從設(shè)計(jì)、書寫方式、API、生命周期及熱門生態(tài)做一下簡(jiǎn)單的對(duì)比:

設(shè)計(jì)

react vue 說明
定位 構(gòu)建用戶界面的js庫 漸進(jìn)式框架 react側(cè)重于library,vue側(cè)重于framework
渲染 setState更新state的值來達(dá)到重新render視圖 響應(yīng)式數(shù)據(jù)渲染,修改了響應(yīng)式數(shù)據(jù)對(duì)應(yīng)的視圖也進(jìn)行渲染 react需要考慮何時(shí)setState,何時(shí)render;vue只需要考慮修改數(shù)據(jù)
編寫方式 jsx template react是函數(shù)式,all in js;vue區(qū)分tempalte、script、style,提供語法糖,使用vue-loader編譯

組件通信

react:嚴(yán)格的單向數(shù)據(jù)流

  • 向下 props
  • 向上 props func
  • 多級(jí)傳遞 context

遵循萬物皆可props,onChange/setState()

vue:?jiǎn)蜗驍?shù)據(jù)流

  • 向下 props down
  • 向上 events up(訂閱發(fā)布)
  • 多級(jí)傳遞 $attrs、$listeners

還有各種獲取組件實(shí)例(VueComponent),如:$refs、$parent、$children;通過遞歸獲取上級(jí)或下級(jí)組件,如:findComponentUpward、findComponentsUpward;高階組件:provide/reject,dispatch/broadcast

react vue 說明
子組件數(shù)據(jù)傳遞 props props 都是聲明式
組件狀態(tài)機(jī) state data 管理組件的狀態(tài),react使用setState更改,vue直接賦值,新屬性使用$set;vue使用函數(shù)閉包特性,保證組件data的獨(dú)立性,react本就是函數(shù)

生命周期

react vue 說明
數(shù)據(jù)的初始化 constructor created
掛載 componentDidMount mounted dom節(jié)點(diǎn)已經(jīng)生成
更新 componentDidUpdate updated react:組件更新完畢后,react只會(huì)在第一次初始化成功會(huì)進(jìn)入componentDidmount,之后每次重新渲染后都會(huì)進(jìn)入這個(gè)生命周期,這里可以拿到prevProps和prevState,即更新前的props和state。 vue:在數(shù)據(jù)更改導(dǎo)致的虛擬 DOM 重新渲染和更新完畢之后被調(diào)用
卸載 componentWillUnmount destroyed 銷毀事件

事件處理

react

  • React 事件的命名采用小駝峰式(camelCase),而不是純小寫
  • 使用 JSX 語法時(shí)你需要傳入一個(gè)函數(shù)作為事件處理函數(shù),而不是一個(gè)字符串
  • 不能通過返回 false 的方式阻止默認(rèn)行為。你必須顯式的使用 preventDefault
  • 不能卸載非Element標(biāo)簽上,否則會(huì)當(dāng)成props傳下去
function Form() {
  function handleSubmit(e) {
    e.preventDefault();
    console.log('You clicked submit.');
  }
  return (
    <form onSubmit={handleSubmit}>
      <button type="submit">Submit</button>
    </form>
  );
}

vue

用在普通元素上時(shí),只能監(jiān)聽原生 DOM 事件。用在自定義元素組件上時(shí),也可以監(jiān)聽子組件觸發(fā)的自定義事件

//原生事件
<form v-on:submit.prevent="onSubmit"></form>
//自定義事件
<my-component @my-event="handleThis(123, $event)"></my-component>

vue事件修飾符:

  • .stop - 調(diào)用 event.stopPropagation()。
  • .prevent - 調(diào)用 event.preventDefault()。
  • .capture - 添加事件偵聽器時(shí)使用 capture 模式。
  • .self - 只當(dāng)事件是從偵聽器綁定的元素本身觸發(fā)時(shí)才觸發(fā)回調(diào)。
  • .native - 監(jiān)聽組件根元素的原生事件。
  • .once - 只觸發(fā)一次回調(diào)。
  • .left - (2.2.0) 只當(dāng)點(diǎn)擊鼠標(biāo)左鍵時(shí)觸發(fā)。
  • .right - (2.2.0) 只當(dāng)點(diǎn)擊鼠標(biāo)右鍵時(shí)觸發(fā)。
  • .middle - (2.2.0) 只當(dāng)點(diǎn)擊鼠標(biāo)中鍵時(shí)觸發(fā)。
  • .passive - (2.3.0) 以 { passive: true } 模式添加偵聽器

class和style

class

react

render() {
  let className = 'menu';
  if (this.props.isActive) {
    className += ' menu-active';
  }
  return <span className={className}>Menu</span>
}

vue

<div
  class="static"
  :class="{ active: isActive, 'text-danger': hasError }"
></div>

 
<div :class="[{ active: isActive }, errorClass]"></div>

style

react

<div style={{color: 'red', fontWeight: 'bold'}} />

vue

<div :style="[baseStyles, overridingStyles]"></div>

組件樣式的時(shí)候你可以在style標(biāo)簽上聲明一個(gè)scoped來作為組件樣式隔離標(biāo)注,如:<style lang="sass" scoped></style>。最后打包時(shí)其實(shí)樣式都加入一個(gè)hash的唯一值,避免組件間css污染

條件渲染

  • react:jsx表達(dá)式, &&或者三元表達(dá)式;return false表示不渲染
  • vue:表達(dá)式返回true被渲染,可嵌套多個(gè)v-else-if,v-else

列表渲染

react:使用.map,一個(gè)元素的 key 最好是這個(gè)元素在列表中擁有的一個(gè)獨(dú)一無二的字符串

<ul>
  {props.posts.map((post) =>
    <li key={post.id}>
      {post.title}
    </li>
  )}
</ul>

vue:為了給 Vue 一個(gè)提示,以便它能跟蹤每個(gè)節(jié)點(diǎn)的身份,從而重用和重新排序現(xiàn)有元素,你需要為每項(xiàng)提供一個(gè)唯一 key attribute

<li v-for="item in items" :key="item.message">
  {{ item.message }}
</li>

組件嵌套

react

默認(rèn)插槽

<div className={'FancyBorder FancyBorder-' + props.color}>
  {props.children}
</div>

具名插槽

<div className="SplitPane">
  <div className="SplitPane-left">
    {props.left}
  </div>
  <div className="SplitPane-right">
    {props.right}
  </div>
</div>

<SplitPane left={<Contacts />} right={<Chat />} />

vue

默認(rèn)插槽

<main>
  <slot></slot>
</main>

具名插槽

<header>
  <slot name="header"></slot>
</header>

獲取DOM

react:管理焦點(diǎn),文本選擇或媒體播放。觸發(fā)強(qiáng)制動(dòng)畫。集成第三方 DOM 庫

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}

vue:被用來給元素或子組件注冊(cè)引用信息

<div ref="div">hello</div>

this.$refs.p.offsetHeight

文檔結(jié)構(gòu)

Umi

├── config                   # umi 配置,包含路由,構(gòu)建等配置
│   ├── config.ts            # 項(xiàng)目配置.umirc.ts優(yōu)先級(jí)更高,使用此方式需要?jiǎng)h除.umirc.ts
│   ├── routes.ts            # 路由配置
│   ├── defaultSettings.ts   # 系統(tǒng)配置
│   └── proxy.ts             # 代理配置
├── mock                     # 此目錄下所有 js 和 ts 文件會(huì)被解析為 mock 文件
├── public                   # 此目錄下所有文件會(huì)被copy 到輸出路徑,配置了hash也不會(huì)加
├── src
│   ├── assets               # 本地靜態(tài)資源
│   ├── components           # 業(yè)務(wù)通用組件
│   ├── e2e                  # 集成測(cè)試用例
│   ├── layouts              # 約定式路由時(shí)的全局布局文件
│   ├── models               # 全局 dva model
│   ├── pages                # 所有路由組件存放在這里
│   │   └── document.ejs     # 約定如果這個(gè)文件存在,會(huì)作為默認(rèn)模板
│   ├── services             # 后臺(tái)接口服務(wù)
│   ├── utils                # 工具庫
│   ├── locales              # 國(guó)際化資源
│   ├── global.less          # 全局樣式
│   ├── global.ts            # 全局 JS
│   └── app.ts               # 運(yùn)行時(shí)配置文件,比如修改路由、修改 render 方法等
├── README.md
└── package.json

vue_cli

├── mock                       # 項(xiàng)目mock 模擬數(shù)據(jù)
├── public                     # 靜態(tài)資源
│   └── index.html             # html模板
├── src                        # 源代碼
│   ├── api                    # 所有請(qǐng)求
│   ├── assets                 # 主題 字體等靜態(tài)資源
│   ├── components             # 全局公用組件
│   ├── directive              # 全局指令
│   ├── filters                # 全局 filter
│   ├── layout                 # 全局 layout
│   ├── router                 # 路由
│   ├── store                  # 全局 store管理
│   ├── utils                  # 全局公用方法
│   ├── views                  # views 所有頁面
│   ├── App.vue                # 入口頁面
│   └── main.js                # 入口文件 加載組件 初始化等
├── tests                      # 測(cè)試
├── vue.config.js              # vue-cli 配置如代理,壓縮圖片
└── package.json               # package.json

路由

動(dòng)態(tài)路由&路由傳參

react-router

  • history.push(/list?id=${id})
  • history.push({pathname: '/list', query: {id}})
  • history.push(/list/id=${id})
  • history.push({pathname: '/list', params: {id}})

獲取 props.match.query / props.match.params

vue-router

  • this.$router.push({path: '/list', query: {id}})
  • this.$router.push({path: '/list', params: {id}})

獲取 this.$router.query / this.$router.params

嵌套路由

-react

{
  path: '/',
  component: '@/layouts/index',
  routes: [
    { path: '/list', component: 'list' },
    { path: '/admin', component: 'admin' },
  ],
}

<div style={{ padding: 20 }}>{ props.children }</div>

使用props.children渲染子路由

vue-router

{
  path: '/user/:id',
  component: User,
  children: [
    {
      path: 'profile',
      component: UserProfile
    },
    {
      path: 'posts',
      component: UserPosts
    }
  ]
}

<div id="app">
  <router-view></router-view>
</div>

使用vue原生組件/<router-view/>組件渲染子路由

路由跳轉(zhuǎn)

umi

<NavLink exact to="/profile" activeClassName="selected">Profile</NavLink>
history.push(`/list?id=${id}`)

vue

<router-link to="/about">About</router-link>
this.$router.push({path: '/list', query: {id}})

路由守衛(wèi)(登錄驗(yàn)證,特殊路由處理)

  • Umi
  • vue-router

全局路由守衛(wèi)

全局前置守衛(wèi):router.beforeEach

 const router = new VueRouter({ ... })
 router.beforeEach((to, from, next) => {
   // ...
 })

全局后置守衛(wèi):router.beforeEach

 router.afterEach((to, from) => {
   // ...
 })

狀態(tài)管理

多個(gè)視圖依賴于同一狀態(tài)或來自不同視圖的行為需要變更同一狀態(tài);才需要使用狀態(tài)管理機(jī)。

dva vuex 說明
模塊 namespace modules 解決應(yīng)用的所有狀態(tài)會(huì)集中到一個(gè)比較大的對(duì)象,store對(duì)象可能變得相當(dāng)臃腫
單一狀態(tài)樹 state state 唯一數(shù)據(jù)源
提交狀態(tài)機(jī) reducer mutations 用于處理同步操作,唯一可以修改 state 的地方
處理異步操作 effects action 調(diào)用提交狀態(tài)機(jī)更改狀態(tài)樹

使用

dva: model connect UI

// new model:models/products.js
export default {
  namespace: 'products',
  state: [],
  reducers: {
    'delete'(state, { payload: id }) {
      return state.filter(item => item.id !== id);
    },
  },
};
//connect model
export default connect(({ products }) => ({
  products,
}))(Products);

//dispatch model reduce
dispatch model reduce({
  type: 'products/delete',
  payload: id,
})

如有異步操作,如ajax請(qǐng)求,dispath model effects,然后effects調(diào)用model reduce
vuex

// new module
const store = new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})
//bind UI
<input v-model="$store.state[modelesName].name"/>
//commit module mutation 
store.commit('increment')

如有異步操作,如ajax請(qǐng)求,dispath module actions,然后actions調(diào)用module mutations

store.dispatch({
  type: 'incrementAsync',
  amount: 10
})

到此這篇關(guān)于vue轉(zhuǎn)react入門指南的文章就介紹到這了,更多相關(guān)vue轉(zhuǎn)react內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 淺談vue自定義全局組件并通過全局方法 Vue.use() 使用該組件

    淺談vue自定義全局組件并通過全局方法 Vue.use() 使用該組件

    本篇文章主要介紹了vue自定義全局組件并通過全局方法 Vue.use() 使用該組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • vue3.x項(xiàng)目中,出現(xiàn)紅色波浪線問題及解決

    vue3.x項(xiàng)目中,出現(xiàn)紅色波浪線問題及解決

    這篇文章主要介紹了vue3.x項(xiàng)目中,出現(xiàn)紅色波浪線問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 關(guān)于Less在Vue中的使用方法介紹

    關(guān)于Less在Vue中的使用方法介紹

    最近發(fā)現(xiàn)好多小伙伴在面試的過程中會(huì)問到vue如何使用less和scss,所以下面這篇文章主要給大家介紹了關(guān)于Less在Vue中的使用方法,需要的朋友可以參考下
    2023-10-10
  • vue react中的excel導(dǎo)入和導(dǎo)出功能

    vue react中的excel導(dǎo)入和導(dǎo)出功能

    當(dāng)我們把信息化系統(tǒng)給用戶使用時(shí),用戶經(jīng)常需要把以前在excel里錄入的數(shù)據(jù)導(dǎo)入的信息化系統(tǒng)里,這樣為用戶提供了很大的方便,這篇文章主要介紹了vue中或者react中的excel導(dǎo)入和導(dǎo)出,需要的朋友可以參考下
    2023-09-09
  • 圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟

    圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟

    本次小編給大家?guī)淼氖顷P(guān)于用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-02-02
  • vuejs中使用mixin局部混入/全局混入的方法詳解

    vuejs中使用mixin局部混入/全局混入的方法詳解

    混入可以省很多代碼(高類聚低耦合),還方便維護(hù),下面這篇文章主要給大家介紹了關(guān)于vuejs中使用mixin局部混入/全局混入的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式

    Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式

    這篇文章主要介紹了Nuxt封裝@nuxtjs/axios請(qǐng)求后端數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue實(shí)現(xiàn)前進(jìn)刷新后退不刷新效果

    vue實(shí)現(xiàn)前進(jìn)刷新后退不刷新效果

    這篇文章主要介紹了vue實(shí)現(xiàn)前進(jìn)刷新,后退不刷新效果,即加載過的界面能緩存起來(返回不用重新加載),關(guān)閉的界面能被銷毀掉(再進(jìn)入時(shí)重新加載)。本文給大家分享實(shí)現(xiàn)思路,需要的朋友可以參考下
    2018-01-01
  • 在Vue中用canvas實(shí)現(xiàn)二維碼和圖片合成海報(bào)的方法

    在Vue中用canvas實(shí)現(xiàn)二維碼和圖片合成海報(bào)的方法

    這篇文章主要介紹了在Vue中用canvas實(shí)現(xiàn)二維碼和圖片合成海報(bào)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • vue動(dòng)態(tài)注冊(cè)組件實(shí)例代碼詳解

    vue動(dòng)態(tài)注冊(cè)組件實(shí)例代碼詳解

    寫本篇文章之前其實(shí)也關(guān)注過vue中的一個(gè)關(guān)于加載動(dòng)態(tài)組件is的API,最開始研究它只是用來實(shí)現(xiàn)一個(gè)tab切換的功能,需要的朋友可以參考下
    2019-05-05

最新評(píng)論