The House Foreign Affairs Committee yesterday approved legislation that would ban computer sales and all new U.S. loans to and investment in South Africa, setting the scene for an early debate on the floor over the imposition of economic sanctions on that nation.

The bill, introduced by Rep. William H. Gray III (D-Pa.) with 145 cosponsors, was approved 29 to 6, with four Republicans joining the Democratic majority, after a GOP bill putting off consideration of sanctions for at least three years was easily defeated.

The Reagan administration, which Wednesday imposed a trade embargo on Nicaragua, opposes the imposition of economic sanctions on South Africa. The apparent inconsistency in its policy was repeatedly noted during the debate over the Republican bill and another over the same issue in the Senate Foreign Relations Committee.

"If total sanctions are justified against Nicaragua," Rep. Stephen J. Solarz (D-N.Y.) said, "can we really say that partial sanctions -- sanctions that would leave undisturbed the overwhelming majority of our bilateral trade -- are not justified against South Africa?"

The administration's answer came from Assistant Secretary of State Chester A. Crocker, who appeared before the Senate Foreign Relations Committee to argue for a "case-by-case" approach and a distinction between South Africa, where he said conditions were improving, and Nicaragua, where he said they were getting worse.

Crocker angered Democratic committee members by charging that many supporters of sanctions were using South Africa as "the moral equivalent of a free lunch."

"That's . . . an unfair putdown," said Sen. Paul S. Sarbanes (D-Md.). "You don't accuse Randy Robinson of being on a free lunch, do you?"

Crocker said no, but indicated that he thought others were.

Randall Robinson is executive director of TransAfrica and a chief organizer of nationwide protests against South Africa, including the picketing of its embassy here.

Crocker defended the administration against charges that its policy of "constructive engagement" with South Africa was bankrupt and that it offered no means of stepping up pressure on the Pretoria government while Congress and the American public were pressing for action.

He said the administration was open to "constructive changes" and that it "shares the goals" of a bill introduced last week by committee Chairman Richard G. Lugar (R-Ind.), Senate Majority Leader Robert J. Dole (R-Kan.) and Sen. Charles McC. Mathias Jr. (R-Md.).

The Lugar-Dole-Mathias bill seeks to increase U.S. support for the black population in South Africa but also calls for making mandatory rather than voluntary the Sullivan principles, ensuring equal treatment for nonwhites in U.S. firms operating there.

Crocker expressed readiness to work with the bill's authors and said the administration's criticism of all bills before the Senate would be sent to the committee today.