日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
React.JS中JSX的原理與關(guān)鍵實(shí)現(xiàn)

 在開始開發(fā)之前,我們需要?jiǎng)?chuàng)建一個(gè)空項(xiàng)目文件夾。

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到莫力達(dá)網(wǎng)站設(shè)計(jì)與莫力達(dá)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋莫力達(dá)地區(qū)。

安裝

初始化

 
 
 
 
  1. npm init -y

2.安裝webpack相關(guān)依賴

 
 
 
 
  1. npm install webpack webpack-cli -D

3.安裝babel-loader相關(guān)依賴

 
 
 
 
  1. npm install babel-loader @babel/core @babel/preset-env -D

4.安裝jsx支持依賴

 
 
 
 
  1. npm install @babel/plugin-transform-react-jsx -D

配置

1.在根目錄下創(chuàng)建main.js文件 此文件為入口文件。

2.在項(xiàng)目根目錄下創(chuàng)建webpack.config.js

 
 
 
 
  1. module.exports={
  2.   entry:{
  3.     main:'./main.js'
  4.   },
  5.   module:{
  6.     rules:[
  7.       {
  8.         test:/\.js$/,
  9.         use:{
  10.           loader:'babel-loader',
  11.           options:{
  12.             presets:['@babel/preset-env'],
  13.             plugins:[['@babel/plugin-transform-react-jsx',{pragma:'createElement'}]] // 自定義設(shè)置pragma參數(shù),我也可以設(shè)置為我的名字:maomin
  14.           }
  15.         }
  16.       }
  17.     ]
  18.   },
  19.   mode:'development',
  20.   optimization:{
  21.     minimize: false

3.創(chuàng)建一個(gè)reactJsx.js文件 此文件為主要邏輯文件。

開發(fā)

reactJsx.js

 
 
 
 
  1. // 封裝創(chuàng)建Dom節(jié)點(diǎn)
  2. class ElementWrapper {
  3.   constructor(type) {
  4.     this.root = document.createElement(type);
  5.   }
  6.   setAttibute(name, value) {
  7.     this.root.setAttibute(name, value);
  8.   }
  9.   appendChild(component) {
  10.     this.root.appendChild(component.root);
  11.   }
  12. }
  13. // 封裝插入文本節(jié)點(diǎn)
  14. class TextWrapper {
  15.   constructor(content) {
  16.     this.root = document.createTextNode(content);
  17.   }
  18. }
  19. // 組件
  20. export class Component {
  21.   constructor() {
  22.     this.props = Object.create(null); // 創(chuàng)建一個(gè)原型為null的空對(duì)象
  23.     this.children = [];
  24.     this._root = null;
  25.   }
  26.   setAttribute(name, value) {
  27.     this.props[name] = value;
  28.   }
  29.   appendChild(component) {
  30.     this.children.push(component);
  31.   }
  32.   get root() { // 取值
  33.     if (!this._root) {
  34.       this._root = this.render().root;
  35.     }
  36.     return this._root;
  37.   }
  38. }
  39. // 創(chuàng)建節(jié)點(diǎn),createElement對(duì)照 webapck.config.js 中pragma參數(shù)。
  40. export function createElement(type, attributes, ...children) {
  41.   let e;
  42.   if (typeof type === "string") {
  43.     e = new ElementWrapper(type);
  44.   } else {
  45.     e = new type();
  46.   }
  47.   for (let p in attributes) { // 循環(huán)屬性
  48.     e.setAttribute(p, attributes[p]);
  49.   }
  50.   let insertChildren = (children) => {
  51.     for (let child of children) {
  52.       if (typeof child === "string") {
  53.         child = new TextWrapper(child);
  54.       }
  55.       if (typeof child === "object" && child instanceof Array) {
  56.         insertChildren(child); // 遞歸
  57.       } else {
  58.         e.appendChild(child);
  59.       }
  60.     }
  61.   };
  62.   insertChildren(children);
  63.   return e;
  64. }
  65. // 添加到Dom中
  66. export function render(component, parentElement) {
  67.   parentElement.appendChild(component.root);
  68. }

main.js

import {createElement,Component,render} from './reactJsx.js'class MyComponent extends Component { render(){ return

maomin

  
 
 
 
  1. import {createElement,Component,render} from './reactJsx.js'
  2. class MyComponent extends Component {
  3.   render(){
  4.     return 
  5.       

    maomin

  6.       {this.children}
  7.     
  •   }
  • }
  • render(
  •   
    xqm
  •   
    my girlfriend
  • ,document.body)
  • 執(zhí)行

     
     
     
     
    1. npx webpack

    在dist文件夾下創(chuàng)建html文件,然后引入main.js,打開html文件就可以看到效果了。


    分享題目:React.JS中JSX的原理與關(guān)鍵實(shí)現(xiàn)
    標(biāo)題URL:http://www.5511xx.com/article/ccdssps.html