+
80
-

vue多语言切换怎么实现?

请问vue多语言切换怎么实现?

网友回复

+
0
-

可以使用vue-i18n,示例代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue@2.6.1.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue-i18n.js"></script>
    <style>
    </style>
</head>
<body>
    <div id="app">
        <p style="font-size:12px;color:#515a6e;">
            {{$t('hello')}} 
        </p>
    </div>

    <script type="text/javascript">

        const i18nMessages = {
            'zh_CN': {
                'hello': '你好',
            },
            'en_US': {
                'hello': 'hello',
            }
        };
        var i18n = new  VueI18n({
            locale: 'zh_CN',
            messages: i18nMessages
        })

        var vm = new Vue({
            el: '#app',
            i18n,
            data: {},
            methods: {},
            beforeCreate: function () {},
            created: function () {}
        });

    </script>
</body>
</html>

我知道答案,我要回答