0

我使用这个包https://www.npmjs.com/package/vue-sweetalert2并且我想将 Vue.swal 中的 vue 组件作为 html 过去。

<template>
    <div class="w-full">
        <div class="py-6 w-full">
            <button
                type="button"
                class="btn btn-default btn-primary"
                @click="openTeamsModal"
            >
                Teams
            </button>
        </div>
    </div>
</template>

<script>

import Vue from 'vue';
import TeamTable from "./TeamTable";
import VueSweetalert2 from 'vue-sweetalert2'; 
Vue.use(VueSweetalert2);

export default {
    components: {TeamTable},

    props: [
        'resourceName',
        'resourceId',
        'field'
    ],

    data: () => ({
        teams: [],
    }),

    methods: {

        openTeamsModal() {

            Nova.request().get(`/api/competitors/${this.field.seasonId}/${this.field.championshipId}`).then( responce  => {
                console.log(responce)
            });

            Vue.swal({
                title: 'Test',
                html: '<team-table></team-table>',
            });
        }

    },
}
</script>

但什么都没有。我是 VueJs 的新手,但我仍然不完全了解如何将我的组件插入为 html。

4

1 回答 1

0

是的!是可能的!...经过数小时的研究,我成功了。

这是这样的:

您需要在此字符之间包含模板的所有逻辑`

您还需要编辑vue.config.js文件并在 configurewebpack 对象中添加:'vue$':'vue/dist/vue.esm.js'

configureWebpack: {
  resolve: {
    alias: {
      'src': resolveSrc('src'),
      'chart.js': 'chart.js/dist/Chart.js',

      // add this line for include components inside swal alert
      'vue$':'vue/dist/vue.esm.js'
    }

完成此操作后,您必须重新启动项目“ npm run dev

这是我的完整示例,经过测试和工作

openSweet() {
      Vue.component('my-comp', {
          template: `
                <div class="card-content">
                  <div class="span2">
                        <div class="col-sm-6 col-md-2 col-lg-3">
                            <div class="row">
                              <div style="margin-top: 6px;" >
                                <p-switch v-model="switchTrip.state" type="primary" on-text="ON" off-text="OFF" style="justify-content:center"></p-switch>
                                <h5 class="card-title" style="margin-left: 25px;">Recorridos</h5>
                              </div>
                            </div>

                            <div class="row">
                              <div style="margin-top: 6px;" >
                                <p-switch v-model="switchVel.state" type="primary" on-text="ON" off-text="OFF" style="justify-content:center"></p-switch>
                                <h5 class="card-title" style="margin-left: 25px;">Velocidad</h5>
                              </div>
                            </div>

                        </div>
                  </div>
                  <div class="span2">
                        <div class="col-sm-6 col-md-4 col-lg-3">
                            <div class="row">
                              <div >
                                <input type="search" class="form-control input-sm" placeholder="km / h" v-model="vmax">
                                <h5 class="card-title">Vel. Max</h5>
                              </div>
                            </div>

                            <div class="row">
                              <div>
                                <input type="search" class="form-control input-sm" placeholder="minutos" v-model="tol">
                                <h5 class="card-title">Tolerancia</h5>
                              </div>
                            </div>
                        </div>
                  </div>
                </div>
          `,
        data () {
          return {
            switchVel: {
              state: false
            },
            switchEvent: {
              state: false
            },
            switchTrip: {
              state: false
            },
            search: '',
            vmax: '',
            tol: ''
          }
        },
        components: {
            [Button.name]: Button,
            Card,
            PSwitch
        }
      })
      new Vue({
        el: '#modal',
        beforeCreate:  () => {
          swal({
            titleText: "Descarga de Reportes",
            showCancelButton: true,
            cancelButtonText: 'Cancelar',
            confirmButtonText: 'Descargar',
            // confirmButtonAriaLabel: 'glyphicon glyphicon-ok-sign',
            // cancelButtonAriaLabel: 'glyphicon glyphicon-remove-sign',
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            width: 800,
            html: '<div id="modal"><my-comp></my-comp></div>'
          })
        }
      })
    }

希望对你有帮助

来自阿根廷的问候

https://github.com/aledc7

于 2019-08-28T13:46:03.420 回答