JavaScript (+LWC)ã®ãã¥ã¼ããªã¢ã«: ãªãã¼ã·ãä½ã(1)
æ¦è¦
第ä¸åç®ã¯ LWC (ãªã¼ãã³ã½ã¼ã¹ç)ã®ã¤ã³ã¹ãã¼ã«ã¨ãåºæ¬ç使ãæ¹ã¾ã§
åæç¥è
åºæ¬ç㪠JavaScript ã³ã¼ããæ¸ããåæã
ç®æ¬¡
LWC ã¤ã³ã¹ãã¼ã«
Python3 系統ãã¤ã³ã¹ãã¼ã«æ¸ã¿ã® Windows ã®å ´åãWSL ã«ã¤ã³ã¹ãã¼ã«ããæãããã¾ããLWC â¦ã¨è¨ããã Nodejs ã®é¢é£ãªã½ã¼ã¹ã Python2.7 ãè¦æ±ãã¦ããããã§ãã
å°ãLWC ã«ã¤ã³ã¹ãã¼ã«ãã¦èµ·åããå ´åããã¡ã¤ã«æ´æ°ã ãã§ã¯ç»é¢ãæ´æ°ããã¾ãããã©ããããã¡ã¤ã«ç£è¦å¦çãããã¯ãããªããæ§åâ¦
NodeJS ã®ã¤ã³ã¹ãã¼ã«(Windows)
NodeJS ã Windows ã«ã¤ã³ã¹ãã¼ã«ããæ¹æ³ã¯ããã¤ãããã¾ãã
å
¬å¼ãµã¤ã ãããã¦ã³ãã¼ãã»ã¤ã³ã¹ãã¼ã«ããæ¹æ³ã¨ãChocolatey
ã使ãæ¹æ³ãããã¾ãã
MAC 使ããªã Homobrew ã® Windows çã¨è¨ãã°ããããããã§ãããï¼
å ¬å¼ãµã¤ãããã®ã¤ã³ã¹ãã¼ã«
ãã¡ããã LTS çãã¤ã³ã¹ãã¼ã«ããã°OKã§ãã
ã¤ã³ã¹ãã¼ã©ã¼ã¯ã¤ã¨ã¹ãã³ããã¦ããã°å¤§ä¸å¤«ã§ããã
ä¸ã¤ã ã注æç¹ã¯ã»ããã¢ããæå¾ã®ä¸è¨ç»é¢ããTools for Native Modulesãã§ã¯ãã§ãã¯ããã¯ã¹ãå ¥ãã¾ãããã
ã¤ã³ã¹ãã¼ã«ãã¤ã¢ãã°ãéãããã¨ã§è¿½å ã®ã¤ã³ã¹ãã¼ã©ã¼ãã³ã³ã½ã¼ã«ã¢ããªã§èµ·åãã¾ãããããã¤ã¨ã¹ãã³ããã°OKã§ãã
追å ã§ãPython2.7 ãã¤ã³ã¹ãã¼ã«ãã¦ããã¾ãã
ãªã㧠Python
ãã¨ããã¨ãLWC éçºã® ä¸é¨ä¾åã¢ã¸ã¥ã¼ã« ã Python 2.7 ã«ä¾åãã¦ãã¾ã£ã¦ããããã§ããâ¦ã
...ããã±ã¼ã¸è ã£ã¦ããããããã´ã«ã¡ã¨ãæããªããªãç¬éã
https://www.python.org/downloads/ ãåç §ãã¦ãææ°ã® 2.7.XXX ããã¦ã³ãã¼ãï¼ã¤ã³ã¹ãã¼ã«ãã¾ãããã
Chocolatey ã使ã£ãã¤ã³ã¹ãã¼ã«
Chocolatey 㯠Windows 㧠Linux çãªããã±ã¼ã¸ããã¼ã¸ãè¡ããã人ã®ããã®ã³ã³ã½ã¼ã«ã§ãã
å人çã«ã¯ãã£ã¡ããªã¹ã¹ã¡ã§ããâ¦
Chocolatey ã¯ãã¡ãããã¤ã³ã¹ãã¼ã«ãã¾ãã https://chocolatey.org/install
ãã ãããããã¤ã³ã¹ãã¼ã«æ¹æ³ã¯ä»åã®æ¬é¡ããå¤ããã®ã§ãURL ã®å
ãè±èªã§ããé å¼µã£ã¦ãã ããâ¦
ã¤ã³ã¹ãã¼ã«ã³ãã³ãã¯ä¸è¨ã§ãã
choco install nodejs-lts
Python2.7 ãå ¥ãã¦ããã¾ãããã
choco install python2
ã§ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
LWC ã® Ubuntuã(Ubuntu 22.04.1 LTS)ã¸ã¤ã³ã¹ãã¼ã«ãã
LWC 㧠NodeJS ãè¡ãå ´åã説æãã¾ãã
ç¹ã« Windows ç°å¢ãæ±ããããªã人åãã§ããâ¦
pyenv ãã¤ã³ã¹ãã¼ã«ãã
NodeJS ã®ä¸é¨ã©ã¤ãã©ãªã§ Python2.7 ã«ä¾åãããã®ã§ãææ°ã® Python (ãã®è¨äºã®æç¹ã§ 3.11) ã¨å ±åãããããã®è¨å®ãè¡ãã¾ãã
$ curl https://pyenv.run | bash
㧠pyenv (Pythonã®ãã¼ã¸ã§ã³ç®¡çã³ãã³ã)ãã¤ã³ã¹ãã¼ã«ãã¦ãPATHãè¨å®ãã¾ã
$ cat << 'EOF' >> ~/.bashrc export PYENV_ROOT="$HOME/.pyenv" command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init -)" EOF
ããããå¿ è¦ãªã©ã¤ãã©ãªãã¤ã³ã¹ãã¼ã«ãã¦
$ sudo apt update; sudo apt install build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev curl \ libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
Python 2.7 ãã¤ã³ã¹ãã¼ã«ãã¾ãã
$ pyenv install 2.7.18 $ pyenv global 2.7.18 $ pyenv rehash
ããã¨
$ python --version Python 2.7.18
NodeJS ã¤ã³ã¹ãã¼ã«
NodeJS 㯠n
ã³ãã³ã ã§ç®¡çãããã¨ã«ãã¾ãã
$ sudo apt install nodejs npm -y $ sudo npm install n -g $ sudo n stable
ããã¾ã§ã§å®å®çã® nodejs ãã¤ã³ã¹ãã¼ã«ããã¾ãã
ãããªã£ãã apt
ã§ã¤ã³ã¹ãã¼ã«ããã nodejs
ã¯éªéãªã®ã§æ¶ãã¦ãã¾ãã¾ãããã
$ sudo apt purge -y nodejs npm
ãLWC ã®ã»ããã¢ãã
éçºã«ä½¿ãä½æ¥ãã£ã¬ã¯ããªãä½æããnpm init lwr
ãå®è¡ãã¾ãã
ããã¸ã§ã¯ãå㯠lwc_reversi
LWCã·ãã¼ã·ã¨ã§ããã¦ããã¾ãããã
ã¢ããªã±ã¼ã·ã§ã³ã®ã¸ã£ã³ã«ã¯ Single Page App
ã§ãããã¸ã§ã¯ããã³ãã¬ã¼ã㯠LWC
ãé¸æãã¾ãã
LWC(Typescript)
ãããã¾ãããTypescript ã¯å¥è¨èªãªã®ã§ãä»åã¯æ±ãã¾ããã
å®è¡ã§ããããç¶ãã¦ä»¥ä¸ã®ã³ãã³ããå®è¡ãã¾ã
> cd lec_reversi > npm install
ã¡ãªã¿ã«ãPython3 ç³»ãå ¥ã£ã¦ãã¨ãåªå çã«ãã¡ããåç §ãã¦ä¸è¨ã®æ§ãªã¨ã©ã¼åãåºãã®ã§ãæ··ä¹±ãé¿ããæå³ã§ã¯åé¤ããã»ããå®å ¨ã§ãã
npm ERR! ValueError: invalid mode: 'rU' while trying to load binding.gyp npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1 npm ERR! gyp ERR! stack at ChildProcess.onCpExit (D:\OneDrive\workspace\study\LWC_Reversi\lwc_reversi\node_modules\node-gyp\lib\configure.js:351:16) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28) npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:291:12) npm ERR! gyp ERR! System Windows_NT 10.0.22621 npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\OneDrive\\workspace\\study\\LWC_Reversi\\lwc_reversi\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd D:\OneDrive\workspace\study\LWC_Reversi\lwc_reversi\node_modules\node-zopfli-es npm ERR! gyp ERR! node -v v18.13.0 npm ERR! gyp ERR! node-gyp -v v7.1.0 npm ERR! gyp ERR! not ok
æå¾ã«ä»¥ä¸ã®ã³ãã³ãã§éçºãµã¼ããä½æãã¾ãããã
npm run start
èµ·åããã http://localhost:3000
ã«ã¢ã¯ã»ã¹ãã¾ãã
確èªãããã³ã³ã½ã¼ã«ç»é¢ã§ Ctrl + c
ã§ãµã¼ããåæ¢ãã¾ãããã
ç´äºããã® start
ã¯ãããã¯ã·ã§ã³ã¢ã¼ã(ãªãªã¼ã¹ã§ä½¿ãæ³å®)ã§ã®èµ·åã«ãªãã¾ãã
éçºä¸ã¯ npm run dev
ã使ãã¾ãã
LWC ãèªã¿è§£ãã¦ã¿ã
ã§ã¯ä»åº¦ã¯ãã¡ã¤ã«ãæ¸ãæããªããçµæã確èªãã¦ã¿ã¾ãããã
npm run dev
ã§ãã©ã¦ã¶ä¸ã«ã¦ç»é¢ã表示ãã¦ããã¾ãã
LWC ã®åºæ¬ã»ãã
ã¾ãã½ã¼ã¹ãè¦ã¦ã¿ãã¨ãã½ã¼ã¹æ¹è£ ã¯ä¸è¨ã®ããã«ãªã£ã¦ããã
.npmignore
: nodejs ã®ç®¡çããå¤ããã¡ã¤ã«/ãã£ã¬ã¯ããªã®å®ç¾©lwe.config.json
: LWC(ãªã¼ãã³ã½ã¼ã¹ç)ã®å¶å¾¡è¨å®package-lock.json
:npm install
ã§ä½æããããä¾åããã±ã¼ã¸çªå·çãè¨é²ãããã¡ã¤ã«package.json
: nodejs ã®ä¾åããã±ã¼ã¸å¶å¾¡ãã¡ã¤ã«src
assets
recipes-logo.png
: 表示ããã¦ããã¯ãç»å
modules
example
: example ã¢ã¸ã¥ã¼ã«app
: app ã³ã³ãã¼ãã³ãapp.css
: ãã®ã³ã³ãã¼ãã³ãã®ã¿ã«é©ç¨ãããã¹ã¿ã¤ã«ã·ã¼ãapp.html
: ãã®ã³ã³ãã¼ãã³ãã®è¡¨ç¤ºHTMLapp.js
: ãã®ã³ã³ãã¼ãã³ãã®ã³ã³ããã¼ã©ãã¸ãã¯
å®éã«æãå ãã¦æåã確èªãã¦ã¿ã¾ãããã
ããããããã¨ããã§ã app.html
ãå¼ããã
<template> <main> <img src="/public/assets/recipes-logo.png" alt="logo" /> <h1>Hello World!</h1> <p>ããã追å è¡ã§ãããã¾ãï¼</p> </main> </template>
ä¿åããç¬éã«è¡¨ç¤ºã§ãã¾ãã
app.css
ã¹ã¿ã¤ã«ã·ã¼ã㧠p
ã¿ã°ã®è²ãå¤ãã¦ã¿ã¾ããã
main { margin: 30px; display: flex; flex-direction: column; align-items: center; } h1 { color: #1798c1; } /* 追å ã¹ã¿ã¤ã« */ p { color: #ff0000; }
ã¤ãã³ããã³ãã©
ã§ã¯ä»åº¦ã¯ app.js
ãã JavaScript å´ããã®ç»é¢ä¿®æ£ãè¡ãã¾ããããã
ãããã¨ã¯ç»é¢ã§çºçããã¤ãã³ãã§ãJavaScript å´ã§å¤æ°ãæ´æ°ãç»é¢ã«åæ ããã¨ããæµãã§ãã
import { LightningElement, track } from 'lwc'; export default class HelloWorldApp extends LightningElement { /* ãããã */ @track count = 0; handleClick() { this.count++ } /* ããã¾ã§ */ }
app.html
å´ãããã«åããã¦ä¿®æ£ãã¾ã
<template> <main> <p>Click count : {count}</p> <button type="button" onclick={handleClick}>ã¯ãªãã¯</button> </main> </template>
ãã¿ã³ãã¯ãªãã¯ãããã¨ã« handleClick
ãèµ·åããã«ã¦ã³ãããã¦ããã¾ãã
track
ãæå®ããã¨ãJavaScript å´ã® count
ãå¤åãããã³ã«ç»é¢å´ã® {count}
ãé£åãã¦å¤åãããã¨ã«ãªãã¾ãã
ãã³ãã¬ã¼ããã¸ãã¯
IF åå²
表示åå²ã®ãã¸ãã¯ã§ã
import { LightningElement, track } from 'lwc'; export default class HelloWorldApp extends LightningElement { @track count = 0; handleClick() { this.count++ } get over10() { // 10 以ä¸ã§ true ãè¿ãã²ãã¿ã¼ return this.count >= 10; } }
ç»é¢å´ã¯
<template> <main> <p>Click count : {count}</p> <button type="button" onclick={handleClick}>ã¯ãªãã¯</button> <!-- 追å è¡ --> <template if:false={over10}>10 æªæºã§ãâ¦</template> <template if:true={over10}>10 ã¯ãªãã¯ãè¶ ãã¾ããï¼</template> </main> </template>
ã¯ãªãã¯æ°ãå¢ããã¨
ã«ã¼ã表示
ã«ã¼ã表示ã«ã対å¿ãã¦ãã¾ãã
import { LightningElement, track } from 'lwc'; export default class HelloWorldApp extends LightningElement { @track count = 0; @track datalist = []; handleClick() { this.count++ this.datalist.push({ count: this.count, message: `Current is ${this.count}` }); } }
ç»é¢å´ã¯
<template> <main> <p>Click count : {count}</p> <button type="button" onclick={handleClick}>ã¯ãªãã¯</button> <template for:each={datalist} for:item="item"> <div key={item.count}>{item.message}</div> </template> </main> </template>
ããã¨ç»é¢ã¯
注æç¹ã¨ãã¦ã2ç¹å¶éãããã¾ãã
- ã«ã¼ãå 容ç´ä¸ã¯ 1 ã¤ã®ã¿ã°ã§ãããã¨
- ã¿ã°ã«ã¯
key
å±æ§ãè¨å®ãããã¦ãã¼ã¯ãªå¤ãè¨å®ããããã¨
次åã¯ã³ã³ãã¼ãã³ãã®ä½¿ãæ¹ã«ã¤ãã¦è¨åãã¦ããã¾ãã