如何在反应中导入QS

时间:2020-09-09 09:56:38

标签: reactjs axios stringify

我搜索了 [如何在反应中使用qs] ,但是找不到答案。

Qs的官方网站https://www.npmjs.com/package/qs 用法

qs = require('qs');
var assert = require('assert');

var obj = qs.parse('a=c');
assert.deepEqual(obj, { a: 'c' });

var str = qs.stringify(obj);
assert.equal(str, 'a=c');

但是我从没看到require()用于反应。

有人知道这个答案吗?

在axios中传递数组参数时,我需要这个。

3 个答案:

答案 0 :(得分:0)

我的React中有这个相同的问题。

这将起作用:

const qs = require('qs')

这行不通:

import qs from 'qs'

我通过从package-lock.json中删除“ @ types / qs”部分,然后运行来解决了该问题

npm install

答案 1 :(得分:-1)

您不必从项目中卸载 import 'dart:io'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:image_picker/image_picker.dart'; import 'package:permission_handler/permission_handler.dart'; class EditProfilePage extends StatefulWidget { @override _EditProfilePageState createState() => _EditProfilePageState(); } class _EditProfilePageState extends State<EditProfilePage> { bool showPassword = false; String imageUrl; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).scaffoldBackgroundColor, elevation: 1, leading: IconButton( icon: Icon( Icons.arrow_back, color: Colors.orangeAccent, ), onPressed: () { Navigator.pop(context); }, ), ), body: Container( padding: EdgeInsets.only(left: 16, top: 25, right: 16), child: GestureDetector( onTap: () { FocusScope.of(context).unfocus(); }, child: ListView( children: [ Text( "Edit Profile", style: TextStyle(fontSize: 25, fontWeight: FontWeight.w500), ), SizedBox( height: 15, ), Center( child: Stack( children: [ Container( child: (imageUrl != null) ? Image.network(imageUrl) : Image.asset('assets/background.jpg'), width: 130, height: 130, decoration: BoxDecoration( border: Border.all( width: 4, color: Theme.of(context).scaffoldBackgroundColor), boxShadow: [ BoxShadow( spreadRadius: 2, blurRadius: 10, color: Colors.black.withOpacity(0.1), offset: Offset(0, 10)) ], shape: BoxShape.circle, ), ), Positioned( bottom: 0, right: 0, child: Container( height: 40, width: 40, decoration: BoxDecoration( shape: BoxShape.circle, border: Border.all( width: 4, color: Theme.of(context).scaffoldBackgroundColor, ), color: Colors.orangeAccent, ), child: IconButton( icon: Icon(Icons.edit), color: Colors.white, onPressed: () => uploadImage(), ), )), ], ), ), SizedBox( height: 35, ), buildTextField("Full Name", "Yeap Wei Kang", false), buildTextField("E-mail", "jonywk1103@gmail.com", false), buildTextField("Password", "********", true), buildTextField("Location", "Ipoh, Perak", false), SizedBox( height: 35, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ OutlineButton( padding: EdgeInsets.symmetric(horizontal: 50), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20)), onPressed: () { Navigator.pop(context); }, child: Text("CANCEL", style: TextStyle( fontSize: 14, letterSpacing: 2.2, color: Colors.black)), ), RaisedButton( onPressed: () { Navigator.pop(context); }, color: Colors.orangeAccent, padding: EdgeInsets.symmetric(horizontal: 50), elevation: 2, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(20)), child: Text( "SAVE", style: TextStyle( fontSize: 14, letterSpacing: 2.2, color: Colors.white), ), ) ], ) ], ), ), ), ); } Widget buildTextField( String labelText, String placeholder, bool isPasswordTextField) { return Padding( padding: const EdgeInsets.only(bottom: 35.0), child: TextField( obscureText: isPasswordTextField ? showPassword : false, decoration: InputDecoration( suffixIcon: isPasswordTextField ? IconButton( onPressed: () { setState(() { showPassword = !showPassword; }); }, icon: Icon( Icons.remove_red_eye, color: Colors.grey, ), ) : null, contentPadding: EdgeInsets.only(bottom: 3), labelText: labelText, floatingLabelBehavior: FloatingLabelBehavior.always, hintText: placeholder, hintStyle: TextStyle( fontSize: 16, fontWeight: FontWeight.bold, color: Colors.black, )), ), ); } uploadImage() async { final _storage = FirebaseStorage.instance; final _picker = ImagePicker(); PickedFile image; //Check Permissions await Permission.photos.request(); var permissionStatus = await Permission.photos.status; if (permissionStatus.isGranted){ //Select Image image = await _picker.getImage(source: ImageSource.gallery); var file = File(image.path); if (image != null){ //Upload to Firebase var snapshot = await _storage.ref() .child('folderName/imageName') .putFile(file); var downloadUrl = await snapshot.ref.getDownloadURL(); setState(() { imageUrl = downloadUrl; }); } else { print('No Path Received'); } } else { print('Grant Permissions and try again'); } } } 。正确的解决方法是使用:

@types/qs

答案 2 :(得分:-2)

运行:

npm install qs

在脚本中,使用:

import Qs from "qs";