mazey-taro-utils
v1.0.2
Published
Mazey's Taro utils.
Downloads
12
Maintainers
Readme
mazey-taro-utils
Mazey's Taro utils.
Install
Use mazey-taro-utils via npm.
npm install mazey-taro-utils --save
Of course, you can also download this file and serve it yourself. The file locates at the lib/mazey-taro-utils.min.js
.
Quick Start
import { getLoginCodeAsync } from "mazey-taro-utils";
getLoginCodeAsync()
.then((code) => {
console.log(`Login code: ${code}`);
})
.catch((error) => {
console.error(error);
});
Usage
quickToast
Displays a toast message.
Parameters:
msg
(string): The message to display in the toast.
Example Usage:
quickToast('Hello, world!');
quickNavigateTo
Navigates to a specified page.
Parameters:
page
(string): The page to navigate to. This should be the name of the directory under/pages
without the/index
part.params
(object, optional): Query parameters to include in the URL.
Example Usage:
quickNavigateTo('home', { params: { userId: 123 } });
quickRedirectTo
Redirects to a specified page, without keeping the current page in the navigation history.
Parameters:
page
(string): The page to redirect to. This should be the name of the directory under/pages
without the/index
part.params
(object, optional): Query parameters to include in the URL.
Example Usage:
quickRedirectTo('home', { params: { userId: 123 } });
quickScrollTo
Quickly scrolls to a specified element on the page.
Parameters:
selector
(string): The selector of the element to scroll to.duration
(number, optional): The duration of the scroll animation in milliseconds. Default is 300ms.
Example Usage:
quickScrollTo('#elementId', 500);
getEnv
Gets the current environment the application is running in.
Returns: string
- The name of the current environment (e.g.,
'h5'
,'weapp'
, etc.).
Example Usage:
console.log(`Current environment: ${getEnv()}`);
getCurrentPage
Gets the path of the current page.
Returns: string
Example Usage:
console.log(`Current page path: ${getCurrentPage()}`);
getAllParams
Gets all parameters of the current page.
Returns: object
Example Usage:
console.log(`Current page params: ${JSON.stringify(getAllParams())}`);
getQueryParam
Gets the value of a query parameter from the current path.
Parameters:
name
(string): The name of the query parameter.
Returns: string
Example Usage:
const userId = getQueryParam('userId');
console.log(`User ID: ${userId}`);
getWindowSize
Gets the size of the current window.
Returns: object
width
(number): The width of the window.height
(number): The height of the window.ratio
(number): The width-to-height ratio.hwRatio
(number): The height-to-width ratio.
Example Usage:
const { width, height, ratio, hwRatio } = getWindowSize();
console.log(`Window size - Width: ${width}, Height: ${height}, Ratio: ${ratio}, HW Ratio: ${hwRatio}`);
getSystem
Gets the current system information, such as "iOS 14.4".
Returns: string
Example Usage:
const systemInfo = getSystem();
console.log(`System Info: ${systemInfo}`);
getLoginCodeAsync
Calls the interface to get the login credential (code).
Returns: Promise<string>
Example Usage:
getLoginCodeAsync().then(code => {
console.log(`Login code: ${code}`);
}).catch(err => {
console.error(`Failed to get login code: ${err}`);
});
isMiniProgram
Checks if the current environment is a WeChat Mini Program.
Returns: boolean
Example Usage:
if (isMiniProgram()) {
console.log('Running in a WeChat Mini Program');
}
isH5
Determines if the current environment is a web environment (H5).
Returns: boolean
true
if the current environment is H5.false
otherwise.
Example Usage:
if (isH5()) {
console.log('Running in a web environment');
}
isWideScreen
Checks if the device has a wide screen.
Returns: boolean
Example Usage:
if (isWideScreen()) {
console.log('Device has a wide screen');
}
isIOS
Checks if the system is iOS.
Returns: boolean
Example Usage:
if (isIOS()) {
console.log('Running on iOS');
}
isAndroid
Checks if the system is Android.
Returns: boolean
Example Usage:
if (isAndroid()) {
console.log('Running on Android');
}
Contributing
Node.js Version: 16.x
Install Dependencies:
npm i
Development:
npm run dev
Build:
npm run build
Document:
npm run docs
Test:
npm run test
License
This software is released under the terms of the MIT license.