Vant: Dialog 无法使用组件的高级用法,方法调用正常

Created on 25 Jun 2018  ·  4Comments  ·  Source: youzan/vant

抱歉,之前提了一个字体加载的问题,但是因为最近忙,没有去创建一个可提供测试的git,加载的问题我自己做了一个简单的patch解决了,抽空提供一个git看看能不能复现之前的问题,多谢~

下面是自己遇到的一个新问题

Describe the bug
正常引入 Dialog 组件,可以使用方法(Dialog.alert({}))弹出,但是无法使用组件调用。

Environment

  • Device: windows
  • Browser: Chrome
  • Vant Version: 1.1.8

用的是默认的 Vue-cli 创建的测试,vue init webpack test 一路回车,按照快速上手中配置的按需加载。

默认打开页面就会弹出对话框,但是这里测试失败,无法正常显示 Dialog
并且按文档中高级用法是不可用的,只有在全局引入 vant 时才可以正常使用高级用法

下面是按需加载测试代码(失败代码):

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
    <van-dialog v-model="test" title="okok">
      <span>hhhh</span>
    </van-dialog>
  </div>
</template>

<script>
import { Dialog } from 'vant'
export default {
  name: 'App',
  data () {
    return {
      test: true
    }
  },
  components: {
    'van-dialog': Dialog
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

这里全部加载测试成功,可以正常显示组件,前提是必须全局引入 vant 否则按需加载的话,无法在template里使用Dialog

全部加载测试代码(成功代码):
main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import Vant from 'vant'
import 'vant/lib/vant-css/index.css'

Vue.config.productionTip = false
Vue.use(Vant)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})

app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>
    <van-dialog v-model="test" title="okok">
      <span>hhhh</span>
    </van-dialog>
  </div>
</template>

<script>
export default {
  name: 'App',
  data () {
    return {
      test: true
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

Most helpful comment

高级用法需要注册 Dialog 组件的,文档里有写

Vue.use(Dialog);

All 4 comments

高级用法需要注册 Dialog 组件的,文档里有写

Vue.use(Dialog);

如果用高级用法,在你vue页面代码加入Vue.use(Dialog);
这个组件有点特别,其他组件都不用加,就这个需要加Vue.use(Dialog);

但是我看有局部注册的方式, 不好使, 我这不想全局注册他 [Dialog.Component.name]: Dialog.Component,
// 局部注册 export default { components: { [Dialog.Component.name]: Dialog.Component, }, };
这个写法还是不行, 有问题

我试了下, 可以局部注册了, 写法是
components: { VanImage: Image, VanDialog: Dialog.Component, }, // VanDialog 这里就是自定义的名字,

Was this page helpful?
0 / 5 - 0 ratings