Vue 2.0 入门示例识记

1.路由示例

复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
    <div id="app">
        <h1>Hello App!</h1>
        <p>
            <router-link to="/foo">Go to Foo</router-link>
            <router-link to="/bar">Go to Bar</router-link>
        </p>
        <router-view></router-view>
    </div>
    <script>
        const Foo = { template: '<div>foo</div>' }
        const Bar = { template: '<div>bar</div>' }

        const routes = [
            { path: '/foo', component: Foo },
            { path: '/bar', component: Bar }
        ]

        const router = new VueRouter({
            routes // short for `routes: routes`
        })

        const app = new Vue({
            router
        }).$mount('#app')
    </script>
</body>
</html>
复制代码

2.动态编译示例

复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>vue complile example</title>
    <script src="vue.js"></script>
</head>
<body>
    <div id="demo">
    </div>
    <script>
        var res = Vue.compile('<div><h1>{{title}}</h1></div>');
        new Vue({
            el: '#demo',
            data: {
                title: 'vue compile'
            },
            render: res.render
        });
    </script>
</body>
</html>
复制代码

3.动态生成Router-Link示例

复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
    <div id="app">

        <router-link to="/">Home</router-link>
        <dynamic-link></dynamic-link>
        <router-link to="/about">About</router-link>

        <div class="box">
            <router-view></router-view>
        </div>

    </div>
    <script>
        const Home = { template: '<h1>Home</h1>' }
        const Content = { template: '<h1>Content</h1>' }
        const About = { template: '<h1>About</h1>' }

        Vue.component('dynamic-link', {
            template: "<component v-bind:is=\"transformed\" /></component>",
            data() {
                return {
                    text: "<router-link to=\"/content\">Content</router-link>"
                }
            },
            computed: {
                transformed() {
                    return { template: this.text }
                }
            }
        });

        const routes = [
            { path: '/', component: Home },
            { path: '/content', component: Content },
            { path: '/about', component: About },
        ]

        const router = new VueRouter({ routes })

        const app = new Vue({ router }).$mount('#app');

        //app.$router.push({ path: '/about' });
    </script>
</body>
</html>
复制代码

4.动态输出(仅为验证构想,并非正途,仅做参考)

复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
    <div id="app"></div>
    <script>
        const NotFound = { template: '<div><p>Page not found</p></div>' };
        const Home = { template: "<div><p>home page</p><a href=\"javascript:mout('/about')\">About</a></div>" };
        const About = { template: "<div><p>about page</p><a href=\"javascript:mout('/')\">Home</a></div>" };

        function getRouterPath()
        {
            var href = window.location.href;
            var pathName = window.location.pathname;
            var startIndex = href.lastIndexOf(pathName);
            if (href.length > startIndex + pathName.length + 1)
                return href.substr(startIndex + pathName.length + 1);
            else
                return "/";
        }

        function findRoute(currentRoute) {
            for (var index in routes)
            {
                var route = routes[index];
                if (route.path == currentRoute)
                    return route.component;
            }
            return NotFound;
        }

        const routes = [
            { path: '/', component: Home },
            { path: '/about', component: About }
        ]

        const router = new VueRouter({ routes })

        const app = new Vue({
            router: router,
            data: {
                currentRoute: getRouterPath()
            },
            computed: {
                ViewComponent() {
                    return findRoute(this.currentRoute);
                }
            },
            render(createElement) { return createElement(this.ViewComponent) }
        });

        app.$mount('#app');

        function mout(routePath) {
            app.$router.push({ path: routePath });
            window.location.reload();
        }
    </script>
</body>
</html>
复制代码

 4.组件之间通信(通过EventBus,其实就是一个vue对象),除了EventBus还有store,但很鸡肋,用EventBus完全可以代替。store内部只能是有一个state成员,其它成员无法传递

复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>组件通信</title>
    <script src="vue.js"></script>
    <script src="vue-router.js"></script>
</head>
<body>
    <div id="app">
        <com1></com1>
        <com2></com2>
    </div>
    <script>
        const eventBus = new Vue();
        Vue.component('com1', {
            template: "<div><button @click='click'>组件1</button></div>",
            data() {
                return {
                    
                }
            },
            computed: {
                
            },
            methods:{
                click:function(){
                    eventBus.$emit("com2click", "hello com2");
                }
            },
            created() {
                eventBus.$on("com1click", message => {
                    alert("com1click:" + message);
                });
            }
        });

        Vue.component('com2', {
            template: "<div><button @click='click'>组件2</button></div>",
            data() {
                return {
                    
                }
            },
            computed: {
                
            },
            methods:{
                click:function(){
                    eventBus.$emit("com1click", "hello com1");
                }
            },
            created() {
                eventBus.$on("com2click", message => {
                    alert("com2click:" + message);
                });
            }
        });

        const app = new Vue().$mount('#app');
    </script>
</body>
</html>
复制代码

 

posted on   空明流光  阅读(92)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2010-10-20 再谈 asp.net 客户端调用服务器端事件并传递参数

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示