Slog6_使用vue前端框架实现单页应用(SPA)

2018-08-22  本文已影响9人  ArthurSlog_
关注微信公众号“ArthurSlog”

时间 时间 时间 每一份有限的时间里 都饱含无限的价值 需要非常珍惜

开发环境MacOS(High Sierra 10.13.5)

需要的信息:

执行方案

cd ~/Desktop

mkdir node_vue_learningload

cd node_vue_learningload

npm init

sudo npm install koa koa-static

index.js

const serve = require('koa-static');
const Koa = require('koa');
const app = new Koa();

// $ GET /package.json
app.use(serve('.'));

app.listen(3000);

console.log('listening on port 3000');

index.html

<!DOCTYPE html>
<html>
    <head>
    <meta charset="utf-8">
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <title>ArthurSlog</title>
    </head>

    <body>
        <div id="app">{{ message }}</div>

        <script>
        new Vue({
            el: '#app',
            data: {
                message: 'Hello ArthurSlog!'
            }
        })
        </script>
    </body>
</html>
分析一下上面的两份文件代码:
  1. 在 index.js 里,引入 koa 框架和 koa-static 中间件实现了一个 静态web服务器,静态路由至当前路径下的 index.html
  2. 在 index.html 里,引入 vue框架

index.html

<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  1. 在 index.html 里,vue.js 使用简洁的模板语法来声明式地将数据渲染进DOM的系统:

index.html

    <body>
        <div id="app">{{ message }}</div>

        <script>
        new Vue({
            el: '#app',
            data: {
                message: 'Hello ArthurSlog!'
            }
        })
        </script>
    </body>

index.html

<div id="app">{{ message }}</div>

index.html

new Vue({
    el: '#app',
    data: {
        message: 'Hello ArthurSlog!'
    }
})
  1. Vue 在背后做了大量工作。现在数据和 DOM 已经被建立了关联,所有东西都是响应式的

node index.js

Hello ArthurSlog!

欢迎关注我的微信公众号 ArthurSlog

关注微信公众号“ArthurSlog”

如果你喜欢我的文章 欢迎点赞 留言

谢谢

上一篇 下一篇

猜你喜欢

热点阅读