VUEJSから始める
![](https://s1.md5.ltd/image/2bc5d50cd4c99a6b3b2c711471451fc6.png)
Vuejsとは
Vueは
frontend JavaScript framework
建築ウェブサイトとユーザーインターフェースのためになぜVueを使うのか?
何を最初に知っていますか?
Like with any framework, you should be comfortable with the underlying language first. In this case, that is JavaScript
始める
インストール
npm install -g @vue/cli
OR
yarn global add @vue/cli
2 .新規プロジェクトの作成
vue create vue-project
Warning
If you are on Windows using Git Bash with minTTY, the interactive prompts will not work. You must launch the command as
winpty vue.cmd create vue-project
. If you however want to still use the vue create vue-project syntax, you can alias the command by adding the following line to your~/.bashrc
file.alias vue='winpty vue.cmd'
You will need to restart your Git Bash terminal session to pull in the updated bashrc file.
あなたはプリセットを選択するよう求められます.のいずれかを選択することができますデフォルトのプリセットは、基本的なバベル+エスニックセットアップ、または選択“手動で選択機能”を選択する機能を選択します.
![](https://s1.md5.ltd/image/971a28a0448b4f4b9ddf8ce6a52999d7.png)
デフォルトのセットアップはすぐに新しいプロジェクトをプロトタイピングするのに最適ですが、マニュアルのセットアップでは、より多くの生産指向のプロジェクトに必要なオプションを提供します.
![](https://s1.md5.ltd/image/ef381847f78cfd6d82ec2cd339174baa.png)
手動で機能を選択する場合は、プロンプトの終わりには、将来的にそれを再利用することができますプリセットとしての選択を保存するオプションがあります.
プロジェクトの実行
プロジェクトディレクトリを開き、このコマンドを実行します.
npm run serve
予想される出力:![](https://s1.md5.ltd/image/96a999b7068fedb6d421e7a5d9fbf745.png)
あなたのブラウザーのリンクをコピーして、ペーストして、出力を見てください.
http://localhost:8080/
HelloWorldの既定のコンポーネントを使用して再生することもできますが、その前にコンポーネントやルータについて話をしましょう.コンポーネント
コンポーネントは、名前を持つ再利用可能なVueインスタンスです.
Vueコンポーネントの基本レイアウト
ボタンコンポーネントの例を示します.
<template>
<button @click="onClick()" :style="{background: color}" class="btn">{{ text }}</button>
</template>
<script>
export default {
methods: {
onClick() {
console.log("Button Clicked!")
},
},
name: 'Button',
props: {
text: String,
color: String,
},
}
</script>
<style scoped>
.btn {
background: #000;
color: #fff;
}
<style>
コンポーネントは、マークアップのためのテンプレート、どのコンポーネント/データ/メソッドだけでなく、そのコンポーネントのスタイルを含むロジックが含まれます.使用
props
外部コンポーネントからユニークなデータを渡す方法です.上のボタンコンポーネントは、次のように他のコンポーネントで再利用できます.
ヘッダーコンポーネントです.
<template>
<header>
<h2>Title</h2>
<Button :text="Add Task" :color='green'/>
</header>
</template>
<script>
import Button from './Button'
export default {
name: 'Header',
components: {
Button
}
}
</script>
<style scoped>
header {
display: flex;
justify-content: space-between;
align-items: cetner;
margin-bottom: 20px;
}
</style>
ボタンコンポーネントを再利用するには、まずボタンをインポートし、components
インサイドscript
タグ.export default {
name: 'Header',
components: {
Button
}
}
Vueルータ
ルーティングは、ナビゲーション中に不要なリロードなしに単一のページアプリケーションを作成することができます.これを行うにはまずvue routerをインストールする必要があります.
npm install vue-router
ルーティングをしましょう
クリエイト
router
プロジェクトのルートディレクトリのフォルダ.ルータフォルダ内で
index.js
.あるページについてのルータを作成するには最初にコンポーネント(ページ)をインポートする必要があります.
import About from '../views/About'
そして、あなたのページのルータを指定してください.const routes = [
{
path: '/about',
name: 'About',
component: About
}
]
以下は私のインデックスの例です.ルータフォルダ
import {createRouter, createWebHistory} from
'vue-router'
import About from '../views/About'
const routes = [
{
path: '/about',
name: 'About',
component: About
}
]
const router = createRouter({
history: createWebHistory(process.env.
BASE_URL),
routes
})
export default router
次のステップは主にルータを使うことです.あなたのVueプロジェクトのルートフォルダにあるJSファイル.import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
createApp(App)
.use(router)
.mount('#app')
あなたのアプリケーションでルータを含める.Vueテンプレートタグ. <router-view></router-view>
現在、あなたは行ってよいです.単一のWebアプリケーションのコンポーネントで、ルータを使用できます.使用する代わりに:
<a href="/about">About</a>
使用する必要があります.<router-link to="/about">About</router-link>
このポストを訪問していただきありがとうございます.あなたが質問をしたり、何かを言いたい場合は以下のコメントをお気軽に.Reference
この問題について(VUEJSから始める), 我々は、より多くの情報をここで見つけました https://dev.to/cyebukayire/getting-started-with-vuejs-69fテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol