Install mockjs in your projectExecute the following installation command in the project directory npm install mockjs --save The basic process of using mockjs in Vue projectAfter the installation is complete, create a new mock.js in the project's src/utils directory (you can define the directory and file name yourself) to generate mock data. // Import mockjs const Mock = require('mockjs') // Generate simulation data const test = function() { return Mock.mock({ // The value of the attribute list is an array containing 1 to 10 elements 'list|1-10': [{ // The attribute id is a self-incrementing number, starting at 1 and increasing by 1 each time 'id|+1': 1, // Generate random data through placeholder 'name': '@name', 'age': '@natural(18, 100)', 'email': '@email' }] }); } // Map the access URL // This means that when Ajax requests the /mock/test path, the test function will be mapped and executed Mock.mock('/mock/test', test) In the project src/api directory, create MockSrv.js to respond to Ajax requests. import axios from 'axios' import mock from '@/utils/mock' export default { testMock() { return axios.get('/mock/test') } } Request mock data generated by Mock in the component. <script> import MockSrv from '@/api/MockSrv' export default { name: 'App', mounted() { MockSrv.testMock().then(function(resp) { console.log("Mock:", resp.data); }); } } </script> Execution Results Mock Syntax SpecificationData Template Definition (DTD)Each attribute in the data template consists of three parts: attribute name, generation rule, and attribute value: // Attribute name // Generate rule // attribute value 'name|rule': value Data Placeholder Definition (DPD) A placeholder simply takes up a place in the attribute value string and does not appear in the final attribute value. @placeholder@placeholder(parameters[, parameters]) Mock.mock({ name: { first: '@FIRST', middle: '@FIRST', last: '@LAST', full: '@first @middle @last' } }) Mock.mock() Generate simulated data based on data template
Mock.Random() Mock.Random is a tool class used to generate various random data. var Random = Mock.Random Random.email() // => "[email protected]" Mock.mock('@email') // => "[email protected]" Mock.mock( { email: '@email' } ) // => { email: "[email protected]" } The methods in Mock.Random correspond one-to-one to the @ placeholders in the data template. If necessary, you can also extend the methods of Mock.Random and then reference them in the data template through @ extension methods. This is the end of this article about Vue's use of mockjs to generate simulated data cases. For more relevant Vue's use of mockjs to generate simulated data content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to block IP and IP range in Nginx
>>: What are the differences between sql and mysql
Engineering Structure The project is divided into...
First, let’s understand what MySQL is? MySQL is a...
Table of contents 1.Vue.js features: 2.Observer.j...
This article shares the specific code of JavaScri...
Table of contents Preface Core code File shows pa...
Due to business needs, there are often rush purch...
The telnet in the Alpine image has been moved to ...
Vim is a text editor that we use very often in Li...
Table of contents Scenario Requirements glibc ver...
Basic syntax: <input type="hidden" na...
Table of contents Create an image File Structure ...
question CSS fixed positioning position:fixed is ...
Table of contents mysql permission control Permis...
The cause is that the process opens a number of f...
"/" is the root directory, and "~&...