TypeScriptを使う(本当に使ってみるだけの場合。)
TypeScriptとはなんぞや
- 大規模開発言語に近い仕様でソースコードを書くことができる
- コンパイルするとJavaScriptになる
コンパイラをインストールする
$ npm install -g typescript /Users/ホスト名/.nvm/versions/node/v8.11.1/bin/tsserver -> /Users/ホスト名/.nvm/versions/node/v8.11.1/lib/node_modules/typescript/bin/tsserver /Users/ホスト名/.nvm/versions/node/v8.11.1/bin/tsc -> /Users/ホスト名/.nvm/versions/node/v8.11.1/lib/node_modules/typescript/bin/tsc + typescript@2.8.3 added 1 package in 2.891s
プロジェクトフォルダを作成
- コマンド
$ cd htdocs/ $ mkdir typescript-test1 $ cd typescript-test1/ $ touch tscode.ts $ touch show.html $ touch tsconfig.json
- 構成
📁 htdocs └ 📁 typescript-test1 ├ 🗒 tscode.ts ├ 🗒 show.html └ 🗒 tsconfig.json
とりあえず出力して表示までやってみる
わーっと書いてみる
[show.html]
<!DOCTYPE> <html> <head> <meta charset="utf-8"> <style> body { width :500px; padding :10%; } </style> </head> <body> <script type="text/javascript" src="tscode.js"></script> </body> </html>
[tscode.ts]
class Cat{ name:string; sayHello(){ return this.name + "だにゃん😺"; } constructor(name:string){ this.name = name; } } let aki = new Cat("あき"); //letはローカル変数を示す document.body.innerHTML = aki.sayHello();
🙂💬 変数の宣言は汎用的な[var]よりローカル変数である[let]の使用が推奨されているそう.
🙂💭 classとかあるし本当にJavaとかC#とかに近い感じがする.
[tsconfig.json]
{"compileOprions": { "outFile": "jscode.js" }}
🙂💬
・[tsconfig.json]が存在するディレクトリがTypeScriptプロジェクトのルート・フォルダであることを示すことになる.
・上記ではコンパイルした後に出力されるファイル名を指定している.
コンパイルする
環境:VSCode(version 1.23)
- コマンドパレット(shift + ⌘ + P)で「tasks」と入力すると↓が出てくるので「configure task」を選択してください
- ビルドに[tsconfig.json]を選択し、実行(Enter)
- VSCodeのターミナルを表示しておくと以下のような結果が表示されます
- コンパイルが完了すると[tscode.js]が作成されます. 中身はこんな感じ↓
var Cat = /** @class */ (function () { function Cat(name) { this.name = name; } Cat.prototype.sayHello = function () { return this.name + "だにゃん😺"; }; return Cat; }()); var aki = new Cat("あき"); //letはローカル変数を示す document.body.innerHTML = aki.sayHello();
😕💭 若干めんどくさい感じのJSだーって印象...
備考
React&WebPackで実行環境を作るならReact & Webpackがチュートリアルになっているのでさんこうにすると良いです.
続きを読むMySQLからの出力で改行を表示する(PHPで処理)
- これだと、改行が反映されない
<table> @foreach ($items as $items) <tr> <td>{{ $items['comment'] }}</td> </tr> @endforeach </table>
- 修正するとこんな感じになりました
<table> @foreach ($items as $items) <tr> <td> <pre>{{ $items['comment'] }}</pre> </td> </tr> @endforeach </table>
- preだとフォームみたいなのがついちゃうので、以下のスタイルを当てる
pre {border: 0; background-color: transparent;}
まとめ
- preタグを使って整形済みテキストとして表示するとよい
追記
こっちの方が現実的なので追記。
azix.hatenablog.com
MySQLのReInstall
HomeBrewで入れたMySQLのrootパスワードを忘れたので入れ直すことにした……
コマンドリスト1〜順に実行していく
brew remove mysql
brew cleanup
~/Library/LaunchAgents/com.mysql.mysqld.plist
の有無を確認- 3.があった場合 remove する
sudo rm -rf /usr/local/var/mysql
brew install mysql
unset TMPDIR
(TMPDIR環境変数の解除)mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.7.22/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
(パスのバージョンはinstallした時に表示されたものを指定すること)launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
mysql_secure_installation
(セキュリティ関連セットアップ)--> YES/NO形式で答えていく- 設定ファイルによる設定(my.cnf)
6.のinstall結果は以下のようになる(現在の最新は5.7.22)
$ brew install mysql ==> Installing dependencies for mysql: openssl ==> Installing mysql dependency: openssl ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz ==> Caveats A CA file has been bootstrapped using certificates from the SystemRoots keychain. To add additional certificates (e.g. the certificates added in the System keychain), place .pem files in /usr/local/etc/openssl/certs and run /usr/local/opt/openssl/bin/c_rehash This formula is keg-only, which means it was not symlinked into /usr/local, because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/openssl/lib CPPFLAGS: -I/usr/local/opt/openssl/include ==> Summary 🍺 /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB ==> Installing mysql ==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.22.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring mysql-5.7.22.high_sierra.bottle.tar.gz ==> /usr/local/Cellar/mysql/5.7.22/bin/mysqld --initialize-insecure --user=asahinakamura --basedir=/usr/local/Cellar/mysql/5.7.22 --datadir=/usr/local/var/mysql --tmpdir=/tmp ==> Caveats We've installed your MySQL database without a root password. To secure it run: mysql_secure_installation MySQL is configured to only allow connections from localhost by default To connect run: mysql -uroot To have launchd start mysql now and restart at login: brew services start mysql Or, if you don't want/need a background service you can just run: mysql.server start ==> Summary 🍺 /usr/local/Cellar/mysql/5.7.22: 317 files, 234.2MB
12.設定ファイルは左から順に読み込まれる
$ mysql --help | grep my.cnf order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
▷一つずつみていくと /usr/local/etc/my.cnf
だけファイルが有ったのでこれに以下の設定を追記
# デフォルトの文字コード character-set-server = utf8 # ユーザーのパスワードの有効期限(0:無効) default_password_lifetime = 0
▷default_password_lifetimeを無効にするのは、MySQL5.7からユーザパスワードの有効期限がデフォルトで360日になったために360日を過ぎてログインできなくなるのを防ぐための処置です
最後に
起動の確認。
$ mysql -u root -p
以上です。