vue中如何在本地導入js文件


import  {setStore,setUser,getStore,removeStore} from "../../../public/localstory"

 

在導入js文件時,推薦大家可以先看下ES6的有關內容

http://es6.ruanyifeng.com/#docs/module

 

import  {setStore,setUser,getStore,removeStore} from "../../../public/localstory"
//其中 setStore,setUser,getStore,removeStore為js里面的方法

 js文件,里面是創建的一些方法:

function setStore(keyword,content){
		if(!keyword) return;
		localStorage.setItem(keyword,JSON.stringify(content));
	}
	//建立一個user對象
	function setUser(id,userName,accountNumber,password,weiCharNumber,url,inRecord,outRecord){
		
			let user={
				"id": id,
				"userName": userName,
				"accountNumber": accountNumber,
				"password": password,
				"weiCharNumber": weiCharNumber,
				"url": url,
				"inRecord": inRecord,
				// [{
				// 	"friendID": 0,
				// 	"record": []
				// }],
				"outRecord": outRecord
				// [{
				// 	"friendID": 0,
				// 	"record": []
				// }]
			}
			return user;
	}

	
	
	// 取出值的方法
	function getStore( keyword){
		if(!keyword) return;
		else return localStorage.setItem(keyword)
	}
	
	//刪除鍵的方法
	function removeStore(keyword){
		if(!keyword) return;
		localStorage.removeItem(keyword);
	}
	
	export {
		setStore,
		setUser,
		getStore,
		removeStore
	}

  注意:必須將方法名寫入export 中,否則import后找不見方法

  這個知識ES6里的一種,還有其他方法可以實現,可以查看ES6官網

  http://es6.ruanyifeng.com/#docs/module

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM