vueVUE技术与项目

如何获取单个vue文件所生成的页面?

2019-08-16  本文已影响48人  somenzz

我们的开发的过程中会接触不少开源项目,他们都各有所长,偶尔看到有些好的场景,自己想拿来使用,但又不想使用重构整个项目,仅仅只想使用某个组件(vue)那么该如何使用呢?

比如我需要下图中这个页面的布局和实现:

image.png

于是我找到它对应的 vue 文件

其源代码如下所示:

<template>
  <el-dialog width="900px" :title="data?'修改表 '+data.name:'创建表'" :visible.sync="is_visible" @close="on_close">
    <el-form size="small" label-width="60px" style="margin-right: 30px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="表名:" :error="String(errors.name?errors.name:'')">
            <el-input v-model="form_data.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="别名:" :error="String(errors.alias?errors.alias:'')">
            <el-input v-model="form_data.alias"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="自诉:" :error="String(errors.readme?errors.readme:'')">
        <el-input v-model="form_data.readme" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <el-form v-for="(item, index) in form_data.fields" :key="index" size="small" :inline="true" label-width="60px">
      <el-form-item label="字段:" style="margin-right: 0px"></el-form-item>
      <el-form-item style="width: 80px" :error="String(errors.fields&&errors.fields[index].name?errors.fields[index].name:'')">
        <el-input v-model="item.name" placeholder="字段名"></el-input>
      </el-form-item>
      <el-form-item style="width: 80px" :error="String(errors.fields&&errors.fields[index]['alias']?errors.fields[index]['alias']:'')">
        <el-input v-model="item.alias" placeholder="别名"></el-input>
      </el-form-item>
      <el-form-item style="width: 140px" :error="String(errors.fields&&errors.fields[index].type?errors.fields[index]['type']:'')">
        <el-select v-model="item.type" placeholder="选择字段类型">
          <el-option label="字符串" :value="0"></el-option>
          <el-option label="整数" :value="1"></el-option>
          <el-option label="浮点数" :value="2"></el-option>
          <el-option label="日期时间" :value="3"></el-option>
          <el-option label="日期" :value="4"></el-option>
          <el-option label="布尔" :value="5"></el-option>
          <el-option label="IP地址" :value="6"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :error="String(errors.fields&&errors.fields[index].required?errors.fields[index]['required']:'')">
        <el-checkbox v-model="item.required" :disabled="item.is_multi">必填</el-checkbox>
      </el-form-item>
      <el-form-item :error="String(errors.fields&&errors.fields[index].is_multi?errors.fields[index]['is_multi']:'')">
        <el-checkbox v-model="item.is_multi">多值</el-checkbox>
      </el-form-item>
      <el-form-item :error="String(errors.fields&&errors.fields[index].readme?errors.fields[index]['readme']:'')">
        <el-input v-model="item.readme" placeholder="自述" style="width: 176px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" style="margin: 0px 1px" icon="el-icon-plus" @click="on_add_field(item, index)"></el-button>
        <el-button size="mini" type="danger" style="margin: 0px 1px" icon="el-icon-delete" @click="on_delete_filed(item, index)"></el-button>
      </el-form-item>
    </el-form>
    <div v-show="form_data.fields.length==0">
      <el-button size="mini" type="primary" style="margin: 10px 60px" icon="el-icon-plus" @click="on_add_field({}, 0)">添加字段</el-button>
    </div>
    <el-form label-width="60px" style="margin-right: 30px">
      <el-form-item>
        <el-button size="small" style="width: 100%" type="primary" :loading="loading" @click="on_submit">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import master from "@/api";
import { master_s } from "@/api";


export default {
  name: "change-table",
  data() {
    return {
      is_visible: true,
      loading: false,
      form_data: !this.is_create
        ? JSON.parse(JSON.stringify(this.data))
        : {
            name: "",
            alias: "",
            readme: "",
            fields: [
              {
                name: "",
                alias: "",
                readme: "",
                is_multi: false,
                required: false
              }
            ]
          },
      errors: {}
    };
  },
  props: ["is_create", "data"],
  mounted() {
    // this.data.fields.push({"id":121,"name":"xxxxx","alias":null,"readme":"","type":0,"is_multi":false,"required":true})
  },
  methods: {
    on_close() {
      this.$el.parentNode.removeChild(this.$el);
      this.$destroy();
    },
    on_add_field(item, index) {
      this.form_data.fields.splice(index + 1, 0, {
        // id: Math.floor(Math.random() * 10000000 + 10000000),
        name: "",
        alias: "",
        readme: "",
        // type: 0,
        is_multi: false,
        required: false
      });
      if (!this.errors.fields) {
        return;
      }
      this.errors.fields.splice(index + 1, 0, {});
    },
    on_delete_filed(field, index) {
      console.log("this.from_data:", this.form_data);
      this.form_data.fields.splice(index, 1);
      // if (JSON.stringify(this.errors) == "{}" && !this.errors.fields) {
      //   return;
      // }
      if (this.errors.fields) {
        this.errors.fields.splice(index, 1);
      }
    },
    on_submit() {
      if (this.is_create) {
        this.loading = true;
        master_s
          .post("mgmt/table", this.form_data)
          .then(response => {
            this.loading = false;
            this.$message.success("添加成功 请重启服务器");
            this.errors = {};
            this.$emit("add_table", response.data);
            this.on_close();
          })
          .catch(error => {
            this.loading = false;
            this.errors = error.response.data;
          });
      } else {
        master_s
          .put(`mgmt/table/${this.data.name}`, this.form_data)
          .then(response => {
            this.$message.success("修改成功");
            this.errors = {};
            for (let key in this.form_data) {
              this.data[key] = this.form_data[key];
            }
            this.on_close();
          })
          .catch(error => {
            this.errors = error.response.data;
          });
      }
    }
  }
};
</script>

<style scoped>
</style>

它依赖项目的 api 文件夹,还依赖 element-ui。

我不想重构现有的前段设计,只想使用这个小页面。

我查了查官方的解决方案:使用 vue-cli 脚手架提供的 vue serve 功能可以快速原型开发:

可以使用 vue serve 和 vue build 命令对单个 *.vue 文件进行快速原型开发,不过这需要先额外安装一个全局的扩展:

npm install -g @vue/cli-service-global

vue serve 的缺点就是它需要安装全局依赖,这使得它在不同机器上的一致性不能得到保证。因此这只适用于快速原型开发。

Usage: serve [options] [entry]

在开发环境模式下零配置为 .js 或 .vue 文件启动一个服务器


Options:

  -o, --open  打开浏览器
  -c, --copy  将本地 URL 复制到剪切板
  -h, --help  输出用法信息

ue serve 使用了和 vue create 创建的项目相同的默认设置 (webpack、Babel、PostCSS 和 ESLint)。它会在当前目录自动推导入口文件——入口可以是 main.js、index.js、App.vue 或 app.vue 中的一个。你也可以显式地指定入口文件:

vue serve MyComponent.vue

详细使用方法你可以参考官方文档,作为初学者的我深试了多次,还是报错了,主要还是不理解 webpack 打包的原理,想想还是放弃了。

但是我想到 vue 是可以嵌入到 html 页面中的,这样的话,就不需要编译打包这么麻烦了,于是我尝试弄了一下,竟然成功了。

参考 vue , element-ui 快速上手的例子,我把原来的 vue 文件转换成下面的 html 文件,先让界面显示出来,后续再加入 api 的实现 。


<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
  <div id="app">
<template>
  <el-dialog width="900px" :title="data?'修改表 '+data.name:'创建表'" :visible.sync="is_visible" @close="on_close">
    <el-form size="small" label-width="60px" style="margin-right: 30px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="表名:" :error="String(errors.name?errors.name:'')">
            <el-input v-model="form_data.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="别名:" :error="String(errors.alias?errors.alias:'')">
            <el-input v-model="form_data.alias"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="自诉:" :error="String(errors.readme?errors.readme:'')">
        <el-input v-model="form_data.readme" type="textarea"></el-input>
      </el-form-item>
    </el-form>
    <el-form v-for="(item, index) in form_data.fields" :key="index" size="small" :inline="true" label-width="60px">
      <el-form-item label="字段:" style="margin-right: 0px"></el-form-item>
      <el-form-item style="width: 80px" :error="String(errors.fields&&errors.fields[index].name?errors.fields[index].name:'')">
        <el-input v-model="item.name" placeholder="字段名"></el-input>
      </el-form-item>
      <el-form-item style="width: 80px" :error="String(errors.fields&&errors.fields[index]['alias']?errors.fields[index]['alias']:'')">
        <el-input v-model="item.alias" placeholder="别名"></el-input>
      </el-form-item>
      <el-form-item style="width: 140px" :error="String(errors.fields&&errors.fields[index].type?errors.fields[index]['type']:'')">
        <el-select v-model="item.type" placeholder="选择字段类型">
          <el-option label="字符串" :value="0"></el-option>
          <el-option label="整数" :value="1"></el-option>
          <el-option label="浮点数" :value="2"></el-option>
          <el-option label="日期时间" :value="3"></el-option>
          <el-option label="日期" :value="4"></el-option>
          <el-option label="布尔" :value="5"></el-option>
          <el-option label="IP地址" :value="6"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :error="String(errors.fields&&errors.fields[index].required?errors.fields[index]['required']:'')">
        <el-checkbox v-model="item.required" :disabled="item.is_multi">必填</el-checkbox>
      </el-form-item>
      <el-form-item :error="String(errors.fields&&errors.fields[index].is_multi?errors.fields[index]['is_multi']:'')">
        <el-checkbox v-model="item.is_multi">多值</el-checkbox>
      </el-form-item>
      <el-form-item :error="String(errors.fields&&errors.fields[index].readme?errors.fields[index]['readme']:'')">
        <el-input v-model="item.readme" placeholder="自述" style="width: 176px"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" style="margin: 0px 1px" icon="el-icon-plus" @click="on_add_field(item, index)"></el-button>
        <el-button size="mini" type="danger" style="margin: 0px 1px" icon="el-icon-delete" @click="on_delete_filed(item, index)"></el-button>
      </el-form-item>
    </el-form>
    <div v-show="form_data.fields.length==0">
      <el-button size="mini" type="primary" style="margin: 10px 60px" icon="el-icon-plus" @click="on_add_field({}, 0)">添加字段</el-button>
    </div>
    <el-form label-width="60px" style="margin-right: 30px">
      <el-form-item>
        <el-button size="small" style="width: 100%" type="primary" :loading="loading" @click="on_submit">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>




</div>
</body>
  <!-- import Vue before Element -->
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <!-- import JavaScript -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script>
    new Vue({
      el: '#app',
      data: function() {
        return { 
            is_visible: true,
            loading: false,
            form_data: {
                  name: "",
                  alias: "",
                  readme: "",
                  fields: [
                    {
                      name: "",
                      alias: "",
                      readme: "",
                      is_multi: false,
                      required: false
                    }
                  ]
                },
            errors: {}
           }
      },
  props: ["is_create", "data"],
  mounted() {
    // this.data.fields.push({"id":121,"name":"xxxxx","alias":null,"readme":"","type":0,"is_multi":false,"required":true})
  },
  methods: {
    on_close() {
      this.$el.parentNode.removeChild(this.$el);
      this.$destroy();
    },
    on_add_field(item, index) {
      this.form_data.fields.splice(index + 1, 0, {
        // id: Math.floor(Math.random() * 10000000 + 10000000),
        name: "",
        alias: "",
        readme: "",
        // type: 0,
        is_multi: false,
        required: false
      });
      if (!this.errors.fields) {
        return;
      }
      this.errors.fields.splice(index + 1, 0, {});
    },
    on_delete_filed(field, index) {
      console.log("this.from_data:", this.form_data);
      this.form_data.fields.splice(index, 1);
      // if (JSON.stringify(this.errors) == "{}" && !this.errors.fields) {
      //   return;
      // }
      if (this.errors.fields) {
        this.errors.fields.splice(index, 1);
      }
    },
    on_submit() {
      if (this.is_create) {
        this.loading = true;
        master_s
          .post("mgmt/table", this.form_data)
          .then(response => {
            this.loading = false;
            this.$message.success("添加成功 请重启服务器");
            this.errors = {};
            this.$emit("add_table", response.data);
            this.on_close();
          })
          .catch(error => {
            this.loading = false;
            this.errors = error.response.data;
          });
      } else {
        master_s
          .put(`mgmt/table/${this.data.name}`, this.form_data)
          .then(response => {
            this.$message.success("修改成功");
            this.errors = {};
            for (let key in this.form_data) {
              this.data[key] = this.form_data[key];
            }
            this.on_close();
          })
          .catch(error => {
            this.errors = error.response.data;
          });
      }
    }
  }
})
  </script>
</html>



上一篇下一篇

猜你喜欢

热点阅读