我们确定我们有一个文件是common.js
这个文件在根目录的static文件夹下
路径如下
|-根目录/static/common.js
- export function getCookie(cname) {
- var name = cname + "=";
- var ca = document.cookie.split(';');
- for (var i = 0; i < ca.length; i++) {
- var c = ca[i];
- while (c.charAt(0) == ' ') c = c.substring(1);
- if (c.indexOf(name) != -1) {
- return c.substring(name.length, c.length);
- }
- }
- return "";
- }
- export function setCookie(cname, cvalue, exdays) {
- var d = new Date();
- d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
- var expires = "expires=" + d.toGMTString();
- document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/;domain=.baidu.cc";
- // setCookies(cname, cvalue, exdays);
- };
-
- var Domains ={
- baidu:'http://www.baidu.cc',
- };
- export {Domains};
以上代码可以看出“各写各的”,引入时是怎么样呢?
我们引入并打印
- //引入全部
- import * as commonjs from "/static/common";
console.log('commonjs==',commonjs)
我们可以通过上图看到,引入时使用【*】号,可以把所有都引入进来
- //指定名称引入
- import {Domains} from "/static/common";
-
- console.log('Domains==',Domains)
- export function getCookie(cname) {
- var name = cname + "=";
- var ca = document.cookie.split(';');
- for (var i = 0; i < ca.length; i++) {
- var c = ca[i];
- while (c.charAt(0) == ' ') c = c.substring(1);
- if (c.indexOf(name) != -1) {
- return c.substring(name.length, c.length);
- }
- }
- return "";
- }
- export function setCookie(cname, cvalue, exdays) {
- var d = new Date();
- d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
- var expires = "expires=" + d.toGMTString();
- document.cookie = cname + "=" + cvalue + "; " + expires + ";path=/;domain=.baidu.cc";
- // setCookies(cname, cvalue, exdays);
- };
-
- var Domains ={
- baidu:'http://www.baidu.cc',
- };
- export {Domains};
-
- export default{
- name:"default"
- };
- //引入全部
- import * as commonjs from "/static/common";
-
- console.log('commonjs==',commonjs)
你会发现所谓的 【 export default】只是在引入文件的时候多了一个名字为“default”的属性
export default用法vue_彻底理解JavaScript ES6中的import和export_重庆Twistzz的博客-CSDN博客